Creating deftype instance from inside a protocol method

61 views
Skip to first unread message

Konrad Hinsen

unread,
Jan 1, 2010, 5:13:01 PM1/1/10
to Clojure
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.

Hugo Duncan

unread,
Jan 1, 2010, 5:56:41 PM1/1/10
to clo...@googlegroups.com
On Fri, 01 Jan 2010 17:13:01 -0500, Konrad Hinsen
<konrad...@fastmail.net> wrote:

> 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

Konrad Hinsen

unread,
Jan 2, 2010, 6:23:29 AM1/2/10
to clo...@googlegroups.com
On 01.01.2010, at 23:56, Hugo Duncan wrote:

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

Jonas Enlund

unread,
Jan 2, 2010, 1:09:30 AM1/2/10
to clo...@googlegroups.com
(note the .)

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

/Jonas

Jonas Enlund

unread,
Jan 2, 2010, 10:05:38 AM1/2/10
to clo...@googlegroups.com
Sorry, I think my reply got lost...

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

Meikel Brandmeyer

unread,
Jan 2, 2010, 10:46:57 AM1/2/10
to clo...@googlegroups.com
Hi,

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

Rich Hickey

unread,
Jan 2, 2010, 12:16:44 PM1/2/10
to clo...@googlegroups.com

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

Konrad Hinsen

unread,
Jan 3, 2010, 1:54:23 AM1/3/10
to clo...@googlegroups.com
On 02.01.2010, at 18:16, Rich Hickey wrote:

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

Reply all
Reply to author
Forward
0 new messages