Re: refs implement IFn, atoms and agents do not?
On Sun, Oct 4, 2009 at 4:26 PM, Stephen C. Gilardi wrote: > > On Oct 4, 2009, at 5:04 PM, Mark Volkmann wrote: > >> Minor technicality ... Vars are a reference type, but deref and @ don't >> work with them. > > I'm guessing you're thinking of an interaction like this: > > user=> (def a 3) > #'user/a > user=> @a > java.lang.ClassCastException: java.lang.Integer cannot be cast to > clojure.lang.IDeref (NO_SOURCE_FILE:0) Yes, that's what I was thinking about. > However, it's important to remember that the expression "a" does not > evaluate to the Var named "a". The Var named by "a" can be accessed via (var > a) or #'a. > > deref does work with Vars: > > user=> @(var a) > 3 > user=> @#'a > 3 > user=> > > deref works with objects that implement clojure.lang.IDeref and > clojure.lang.Var is among them: > > user=> (isa? clojure.lang.Var clojure.lang.IDeref) > true > user=> Thanks for clarifying that! -- R. Mark Volkmann Object Computing, Inc. --~--~-~--~~~---~--~~ 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: refs implement IFn, atoms and agents do not?
On Oct 4, 2009, at 5:04 PM, Mark Volkmann wrote: Minor technicality ... Vars are a reference type, but deref and @ don't work with them. I'm guessing you're thinking of an interaction like this: user=> (def a 3) #'user/a user=> @a java.lang.ClassCastException: java.lang.Integer cannot be cast to clojure.lang.IDeref (NO_SOURCE_FILE:0) user=> However, it's important to remember that the expression "a" does not evaluate to the Var named "a". The Var named by "a" can be accessed via (var a) or #'a. deref does work with Vars: user=> @(var a) 3 user=> @#'a 3 user=> deref works with objects that implement clojure.lang.IDeref and clojure.lang.Var is among them: user=> (isa? clojure.lang.Var clojure.lang.IDeref) true user=> --Steve smime.p7s Description: S/MIME cryptographic signature
Re: refs implement IFn, atoms and agents do not?
On Sun, Oct 4, 2009 at 3:07 PM, Stephen C. Gilardi wrote: > > On Oct 3, 2009, at 1:50 PM, Stuart Halloway wrote: > >> Is there a principled reason for this? I have written some code that >> (unintentionally) limits itself to refs because it assumes that all >> reference types can sit in function position. > > This discussion: > > http://groups.google.com/group/clojure/browse_thread/thread/534dd074f18851ab > > suggests that reference types implementing IFn isn't the intended behavior. > > Using deref or @ works with all reference types: Minor technicality ... Vars are a reference type, but deref and @ don't work with them. -- R. Mark Volkmann Object Computing, Inc. --~--~-~--~~~---~--~~ 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: refs implement IFn, atoms and agents do not?
On Oct 3, 2009, at 1:50 PM, Stuart Halloway wrote: Is there a principled reason for this? I have written some code that (unintentionally) limits itself to refs because it assumes that all reference types can sit in function position. This discussion: http://groups.google.com/group/clojure/browse_thread/thread/534dd074f18851ab suggests that reference types implementing IFn isn't the intended behavior. Using deref or @ works with all reference types: user> (defn my-fn [x] (+ x 3)) #'user/my-fn user> (def my-agent (agent my-fn)) #'user/my-agent user> (@my-agent 4) 7 --Steve smime.p7s Description: S/MIME cryptographic signature
refs implement IFn, atoms and agents do not?
Is there a principled reason for this? I have written some code that (unintentionally) limits itself to refs because it assumes that all reference types can sit in function position. Thanks, Stu --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---