eval'ing records

15 views
Skip to first unread message

David McNeil

unread,
Apr 14, 2011, 11:15:35 AM4/14/11
to Clojure
I have learned that instances of records do not eval to themselves.
This seems inconsistent to me. I am curious if this is intentional or
if it is a gap in the current record implementation in Clojure.

Thanks.
-David

----

(defn eval-type [x]
(class (eval x)))

;; instances of structural types evaluate to themselves

(eval-type {:a 100})
;; -> clojure.lang.PersistentArrayMap

(eval-type [100])
;; -> clojure.lang.PersistentVector

;; instances of java objects evaluate to themselves

(eval-type "hello")
;; -> java.lang.String

(eval-type (java.util.Date.))
;; -> java.util.Date

(eval-type (java.util.Locale. "en" "US"))
;; -> java.util.Locale

;; but instances of records...

(defrecord Foo [a])

(eval-type (Foo. 100))
;; -> clojure.lang.PersistentArrayMap

;; .. evaluate to maps not instances of the record class?

David McNeil

unread,
Apr 14, 2011, 4:07:34 PM4/14/11
to Ivan Koblik, clo...@googlegroups.com
> I remember reading about it in the Joy of Clojure. It may be fixed in the
> future versions of Clojure.


Ivan - Thanks for the response. I checked Joy of Clojure and I see a
reference at the top of page 191 to the fact that records cannot be
printed and then eval'd. I was aware of this, however the issue I
identified in this discussion is that record instances _themselves_ do
not eval to themselves. This is a bit different than printing them and
eval'ing them.

-David

Ivan Koblik

unread,
Apr 14, 2011, 4:12:08 PM4/14/11
to David McNeil, clo...@googlegroups.com
David,

Sorry, I misunderstood your question at first. There was actually a discussion on it a couple of days ago:

Cheers,
Ivan.

David McNeil

unread,
Apr 16, 2011, 10:27:02 AM4/16/11
to Clojure
For those who duck it in the future, there is more discussion here:
http://groups.google.com/group/clojure-dev/browse_thread/thread/f4907ebca8ef6e11

-David
Reply all
Reply to author
Forward
0 new messages