On Sun, Apr 11, 2010 at 06:39, Sophie <itsme...@hotmail.com> wrote:
> (deftype A [x]) gives me an accessor (:x anA)
>
> Then I decide to change data representation of A without impacting
> client code, but I don't seem able to define a function
> (defn :x [anA] ...)
>
> Should I be doing something different?

It's not deftype that's giving you that accessor. All keywords, when
used as a function of an map try to look themselves up in said map.

(:somefield somemap)
;; means the same as:
(somemap :somefield)
;; provided somemap isn't nil

The things deftype defines behave like maps, which is why (:x anA)
works the way you expect.

You could defined accessor functions yourself and then use only those
to access the internals of your A instances. You'd then only have to
change the accessor function if the representation of A changed.
(deftype may also provide better ways of accomplishing this, I haven't
yet used it in practice.)

(defn x-of-A [anA] (:x anA))

// Ben

-- 
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

To unsubscribe, reply using "remove me" as the subject.

Reply via email to