On Sat, Jan 2, 2010 at 6:23 AM, Konrad Hinsen
<konrad.hin...@fastmail.net> wrote:
> 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.
>

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

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