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

131 views
Skip to first unread message

Shogo Ohta

unread,
Feb 28, 2014, 5:11:54 AM2/28/14
to cloju...@googlegroups.com
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

Alex Miller

unread,
Feb 28, 2014, 5:31:42 AM2/28/14
to cloju...@googlegroups.com
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.

Mikera

unread,
Feb 28, 2014, 7:13:38 PM2/28/14
to cloju...@googlegroups.com
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?

Shogo Ohta

unread,
Feb 28, 2014, 9:14:10 PM2/28/14
to cloju...@googlegroups.com
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:

Alex Miller

unread,
Feb 28, 2014, 9:21:41 PM2/28/14
to cloju...@googlegroups.com, cloju...@googlegroups.com
Happy to see a ticket/patch to consider.

Shogo Ohta

unread,
Feb 28, 2014, 9:36:53 PM2/28/14
to cloju...@googlegroups.com
OK. I'll make a new ticket soon. So, is it a defect or an enhancement?

Alex Miller

unread,
Feb 28, 2014, 11:05:11 PM2/28/14
to cloju...@googlegroups.com, cloju...@googlegroups.com
Enhancement

Shogo Ohta

unread,
Mar 1, 2014, 1:06:49 AM3/1/14
to cloju...@googlegroups.com
I just created the ticket and patch!

http://dev.clojure.org/jira/browse/CLJ-1366
Reply all
Reply to author
Forward
0 new messages