(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.
> I want to create a new instance of a deftype from inside one of its
> methods.
I ended-up using extend-type for this case.
Hugo
>> I want to create a new instance of a deftype from inside one of its
>> methods.
>
> I ended-up using extend-type for this case.
That's probably the cleanest solution, but you lose the performance
benefit of defining methods right in the type definition.
After looking at the deftype code, I came up with the following
solution:
(defprotocol Foo
(foo [x]))
(deftype Bar
[i]
Foo
(foo [] (new Bar (inc i))))
(foo (Bar 0))
Its drawback is relying on an undocumented feature: the value of Foo
inside the methods is the class Foo being defined.
Konrad.
(deftype Bar
[i]
Foo
(foo [] (Bar. (inc i))))
/Jonas
Inside deftype methods the symbol "Bar" is the name of the class. You
can use the constructor (Bar. (inc i)) instead. Again, note the "."
after Bar.
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
Am 02.01.2010 um 12:23 schrieb Konrad Hinsen:
> (deftype Bar
> [i]
> Foo
> (foo [] (new Bar (inc i))))
>
> Its drawback is relying on an undocumented feature: the value of Foo
> inside the methods is the class Foo being defined.
Isn't this only the case for AOT compiled deftype? For dynamic it's Bar_123 or so.
Concerning the performance impact: When used in a homogeneous environment, the extend version will be almost as fast as the directly defined method, if I understood Rich correctly.
Sincerely
Meikel
That's documented:
;from (doc deftype)
"In the method bodies, the (unqualified) name can be used to name the
class (for calls to new, instance? etc)."
Rich
> That's documented:
>
> ;from (doc deftype)
>
> "In the method bodies, the (unqualified) name can be used to name the
> class (for calls to new, instance? etc)."
Thanks, that solves the problem perfectly well!
Konrad.