r/Clojure • u/ApprehensiveIce792 • May 29 '25
Why does `(identical? "a" "a") evaluates to `true`
user>
-------------------------
clojure.core/identical?
Tests if 2 arguments are the same object
nil
user>
true
Also, in this video, it's returning false - https://www.youtube.com/watch?v=ketJlzX-254&t=1169s
14
Upvotes
4
u/frogking May 29 '25
To blow your mind, try this:
I guess it’s a memory managemet thing that ends up doing the right thing?
4
u/daveliepmann May 29 '25
https://clojure.org/guides/equality
2
u/npafitis May 29 '25
JVM will do string interning on string literals automatically. So they are in fact the same object in memory in this case
1
u/therealdivs1210 May 29 '25
This is called interning.
Strings and small integers are interned by the JVM.
Keywords are interned by Clojure.
1
u/joinr May 30 '25
Fun with identity and parsing....
This bit me during some serialization tasks, where true/false where being serialized and then deserialized as above. The problem I ran into was that (Boolean. "false") is technically truthy, since in clojure
falseis actually a specific value, Boolean/FALSE, so anything not identical to that is considered non-false, e.g. truthy. So (bear in mind this was the first time in like 14 years), I ended up with counterintuitive results where a seemingly false value (a (Boolean. "false") boxed result of parsing, which happily printed asfalsein the repl)) was able to pass throughifpredicates as non-false.0
u/[deleted] May 29 '25
[deleted] — view deleted comment
2
u/[deleted] May 29 '25
[removed] — view removed comment