Hi,

On May 31, 9:37 am, "Sina K. Heshmati" <s...@khakbaz.com> wrote:

> The atomic 'state' doesn't seem to be visible to the datatype methods. The 
> question is why?
>
> (defprotocol prot-a
>   (op-a [self x y]))
>
> (let [state (atom 10)]
>   (deftype t-a [member]
>     prot-a
>     (op-a [self x y]
>       (+ (.member self) x y @state))))
>
> (def t-a-instance (t-a. 5))

Of course not. deftype defines a type and not an environment. It's
methods are not closures. (And even if they were, your state would be
the same for all instances.) You have to pass the state as an argument
to the constructor.

(defprotocol prot-a ...)
(deftype t-a [state member] ...)

(defn make-t-a [member] (t-a. (atom 0) member))

(def t-a-instance (make-t-a 5))

If you actually want the same state for instances, use a private var.

(defprotocol prot-a ...)
(def ^{:private true} state (atom 0))
(deftype t-a [member] ...)

(def t-a-instance (t-a. 5))

Sincerely
Meikel


Sincerely
Meikel

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