Hacker News new | ask | show | jobs
by LandR 2490 days ago
Also check that person isn't null, probably.

person?.firstName?.trim();

What (s)he means though is in Java if firstName is there then it will be a string.

In Clojure firstName might be anything, a string, a number or even an entire other hashmap or type, literally anything. This might or might not cause a runtime crash if you are doing something that assumes it's a string. So you check.

1 comments

ok so it saves an extra (typeof x) check.

To be honest I rerely see the typeof check in clojure code.

And "saves" is a bit of a misnomer, since it implies the "cost" (of all the static type machinery) is less. Well, dynamic fans (or those with dynamic preferences if "fan" is too strong) will disagree. ;) In practice many systems get streams of bits from somewhere (like the network) that commonly get interpreted into strings and from there other types. The validation and conversion is necessary in any language, after that though it's just FUD to bring up that a function expecting a person with a :name key and string value potentially could be given something else. In the cases where through changes we make it something else, static types are a nice extra assurance on consistency, but that isn't the only way or the most impactful way to gain assurances.