Re: Creating deftype instance from inside a protocol method
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. -- 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
Re: Creating deftype instance from inside a protocol method
On Sat, Jan 2, 2010 at 6:23 AM, Konrad Hinsen 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
Re: Creating deftype instance from inside a protocol method
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 -- 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
Re: Creating deftype instance from inside a protocol method
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. On Sat, Jan 2, 2010 at 1:23 PM, Konrad Hinsen 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. > > 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 -- 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
Re: Creating deftype instance from inside a protocol method
(note the .) (deftype Bar [i] Foo (foo [] (Bar. (inc i /Jonas -- 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
Re: Creating deftype instance from inside a protocol method
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. -- 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
Re: Creating deftype instance from inside a protocol method
On Fri, 01 Jan 2010 17:13:01 -0500, Konrad Hinsen 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 -- 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
Creating deftype instance from inside a protocol method
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