> The problem is you aren't quoting the forms fed to eval.

That was intentional. Imagine a broader example, where I want to read  
in a form, examine/print/etc. parts of it, then evaluate it.

Printing a struct-map produces map syntax.

Reading the output produces a literal map with unevaluated contents.

E.g.,

user=> (read-string "{:foo (fn [x] (+ 1 x)) :bar 2}")
{:foo (fn [x] (+ 1 x)), :bar 2}

;; Now I can examine the syntactic form of the function, but fetch it  
with (:foo x).
;; I can eval this to get a final object.

user=> (eval *1)
{:foo #<user$eval__7$fn__9 user$eval__7$fn...@230c34a9>, :bar 2}


Evaluating the read forms produces a literal map with evaluated  
contents. Modulo unreadable things like functions, the original struct- 
map and the read+eval'ed struct map are identical, but have different  
types.


> This:
>    (eval (struct foo 1 2))
> mean this:
>    (eval {:bar 1 :baz 2})
>
> And small literal maps are eval'd as a PersistentArrayMap

I know. The reason this is a gotcha is that -- just like sorted maps  
and sets -- these data structures are not quite homoiconic. That is,  
reading the printed representation of a data structure returns a  
structure with similar but different properties.

If I wasn't using accessors, it would never have occurred to me that  
my in-memory struct maps would come back as plain ol' maps. It doesn't  
confound me, I just didn't think of it in advance.


> Now if instead you do:
>    (eval '(struct foo 1 2))
> then eval is eval'ing the form (struct foo 1 2), thus the return from
> eval is as you would expect:
>
> user=> (foo-bar (eval '(struct foo 1 2)))

Indeed. That is all basic stuff that a novice Lisper would know.

The scenario here, though, is different: given an *existing* struct- 
map (which prints as {:foo 1 :bar 2} -- not a form), one cannot print  
it in such a way that the struct-map-ness is preserved when it is re- 
read. You can discover that it's a struct-map (type =>  
PersistentStructMap), but not what kind of struct-map it is.

The same is true of sorted maps and sets, and assorted other data  
structures. Clojure's situation is much better than that of Common  
Lisp, and (as I said) I don't regard it as a bug that not all  
attributes of a structure are preserved through printing and re- 
reading, but I thought others might benefit from my experience…  
particularly because accessors would seem to offer additional speed  
for free.

-R
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to