The problem is you aren't quoting the forms fed to eval.
This:
(eval (struct foo 1 2))
mean this:
(eval {:bar 1 :baz 2})
And small literal maps are eval'd as a PersistentArrayMap
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)))
1
On Jul 30, 11:57 am, Richard Newman <[email protected]> wrote:
> I understand what's going on here (and I don't regard it as a bug that
> needs fixing) but I thought I'd share something that caught me off-
> guard, and perhaps should be mentioned in the docs for struct-maps.
>
> Clojure 1.0.0-
> user=> (defstruct foo :bar :baz)
> #'user/foo
> user=> (def foo-bar (accessor foo :bar))
> #'user/foo-bar
> user=> (= (struct foo 1 2) (eval (struct foo 1 2)))
> true
> user=> (foo-bar (struct foo 1 2))
> 1
> user=> (foo-bar (eval (struct foo 1 2)))
> java.lang.ClassCastException: clojure.lang.PersistentArrayMap cannot
> be cast to clojure.lang.PersistentStructMap (NO_SOURCE_FILE:0)
>
> i.e., struct-map-ness does not persist through evaluation.
>
> Client code must either avoid accessors for data that might be
> evaluated, or post-process the result of calling eval. I have a
> defstruct* macro that defines a structure and its accessors, so it's
> straightforward for me to also define a map->foo function that
> rebuilds the struct from the resultant map.
>
> -R
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---