The empty map literal is read as a different object each time

Showing 1-8 of 8 messages
The empty map literal is read as a different object each time Shogo Ohta 2/28/14 2:11 AM
Hi,

I thought {} is always read as the same object (i.e. clojure.lang.PersistentArrayMap/EMPTY). In fact, however, it is read as a different object each time:

  user=> (identical? (read-string "{}") (read-string "{}"))
  false
  user=> (identical? (read-string "{}") clojure.lang.PersistentArrayMap/EMPTY)
  false
  user=>

Although not a problem itself, this causes some weird behaviors (which themselves may also not be a real problem).
For example, the following can happen in REPL, which is ultimately caused by the behavior above.

  user=> (identical? {} {})
  true
  user=> (def x (identical? {} {}))
  #'user/x
  user=> x
  false
  user=>

I'm not sure whether these behaviors should be fixed or we can leave them as they are. Do you have any suggestions?

Thanks,
Shogo

Re: The empty map literal is read as a different object each time Alex Miller 2/28/14 2:31 AM
I think this is the expected behavior. You should expect that the same literal map representation (empty or not) yields equal maps but not necessarily identical maps. In most cases, you should compare collections by equality with =, not by identity. 


--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/groups/opt_out.

Re: The empty map literal is read as a different object each time Mikera 2/28/14 4:13 PM
I agree there shouldn't be a guarantee of identity, but it would be a much better implementation decision to always return the same map if possible. This would be a very sensible optimisation to make.

I'm guessing there might be an underlying reason for different maps though, e.g. different metadata?
Re: The empty map literal is read as a different object each time Shogo Ohta 2/28/14 6:14 PM
I suspect the current implementation incidentally returns different maps. All the other collection literals are always read as the same objects.

  user=> (identical? (read-string "()") (read-string "()"))
  true

  user=> (identical? (read-string "[]") (read-string "[]"))
  true
  user=> (identical? (read-string "#{}") (read-string "#{}"))
  true
  user=>

2014年3月1日土曜日 9時13分38秒 UTC+9 Mikera:
Re: The empty map literal is read as a different object each time Alex Miller 2/28/14 6:21 PM
Happy to see a ticket/patch to consider.
Re: The empty map literal is read as a different object each time Shogo Ohta 2/28/14 6:36 PM
OK. I'll make a new ticket soon. So, is it a defect or an enhancement?
Re: The empty map literal is read as a different object each time Alex Miller 2/28/14 8:05 PM
Enhancement
Re: The empty map literal is read as a different object each time Shogo Ohta 2/28/14 10:06 PM
I just created the ticket and patch!

http://dev.clojure.org/jira/browse/CLJ-1366