I want to create a new instance of a deftype from inside one of its  
methods. A basic example would be

(defprotocol Foo
   (foo [x]))

(deftype Bar
   [i]
   Foo
     (foo [] (Bar (inc i))))

(foo (Bar 0))

This fails with an exception:
java.lang.ClassCastException: java.lang.Class cannot be cast to  
clojure.lang.IFn (NO_SOURCE_FILE:0)

Apparently the value of Bar inside the method definition is not yet  
the value that it will have after the deftype is completed. The  
following workaround works but is not very elegant:

(declare new-Bar)

(deftype Bar
   [i]
   Foo
     (foo [] (new-Bar (inc i))))

(def new-Bar Bar)

(foo (Bar 0))

Does anyone have a better idea?

Konrad.

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