Timothy, Stephen and Stuart, Thanks for three very informative and understandable responses! It's very encouraging to see that such support is available when approaching a new language. I look forward to further exploration of Clojure.
The var/value/symbol relationship continues to be a bit slippery for me but I think I get it. The compiler's differing behaviors when presented with vars vs. identities makes sense now that I think about it. Thanks also for the class-checking syntax, which will come in very handy. Would it make any sense to make @ polymorphic so that @x return x's value when x is a var rather than raising an error? Anand On Jan 18, 3:21 pm, Stuart Sierra <the.stuart.sie...@gmail.com> wrote: > Hi Anand, > > You don't normally need to dereference vars because the compiler does > it for you. > > user> (def x 5) > #'user/x > > This creates a var bound to the symbol "x". But now, whenever you type > "x" the compiler will automatically look up the var and return its > value. > > user> x > 5 > > So when you type (var? x) it's resolved to (var? 5). You're asking > "is 5 a var?" and the answer is no. If you want to get at the var > object itself, you can use the special form (var x) > > user> (var x) > #'user/x > > Note the #' prefix -- that means you're looking at a var object and > not the symbol "user/x". > > user> (var? (var x)) > true > user> (var-get (var x)) > 5 > > I think deref worked on vars in early versions of Clojure, but it > doesn't now. > > If you're just getting started with Clojure, you can probably ignore > vars. You will rarely have any need to work with vars directly unless > you're doing some complicated metaprogramming. > > -Stuart Sierra > > On Jan 18, 5:11 am, "anand.prabhakar.patil" > > <anand.prabhakar.pa...@gmail.com> wrote: > > Hi all, > > I'm new to Java, Lisp and Clojure, so please be patient. I'm trying to make > > a function that behaves as follows: (my-deref x) returns x if x is a var, or > > @x if x is a ref. However none of the obvious options seem to be working. > > > - First, it seems from the api documentation that @x will work fine even if > > x is a var. I don't understand the following: > > > user=> (def x 5) > > #'user/x > > user=> @x > > java.lang.IncompatibleClassChangeError (NO_SOURCE_FILE:0) > > user=> (var? x) > > false > > > or > > > user=> (def x) > > #'user/x > > user=> (var? x) > > false > > > Why isn't x a var? > > > - Second, I remember having seen a ref? function analogous to var? in the > > online documentation, but it doesn't seem to actualy exist: > > > user=> (ref? x) > > java.lang.Exception: Unable to resolve symbol: ref? in this context > > (NO_SOURCE_FILE:13) > > > Thanks in advance, > > Anand --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---