Hi!

> I tried this: (var (symbol name))
> but that caused the exception: clojure.lang.PersistentList cannot be
> cast to clojure.lang.Symbol

var is a special form. If var was a function, (symbol name) would have  
been evaluated and the result would have been passed to var as an  
argument. Special Forms and macros don't necessarily work that way -  
it's up to them to decide how to treat the arguments.

You can use ns-resolve to find a var by name:


user=> (def x 1)
#'user/x

; find the var. *ns* is bound to the current namespace
user=> (ns-resolve *ns* (symbol "x"))
#'user/x

; get the value
user=> (deref (ns-resolve *ns* (symbol "x")))
1

:shorter way of deref'ing
user=> @(ns-resolve *ns* (symbol "x"))
1


clojure.core/ns-resolve
([ns sym])
   Returns the var or Class to which a symbol will be resolved in the
   namespace, else nil.  Note that if the symbol is fully qualified,
   the var/Class to which it resolves need not be present in the
   namespace.


Hope this helps.

Kind regards,
achim



Am 06.03.2009 um 15:23 schrieb timc:

>
> I would like to have a function which tells me about the values of a
> var, where the parameter to the function is a string equal to the
> 'name' of the var.
>
> For example:
>
> (defn checkNil [name]
>     "If the var with the given (String) name is nil, display a
> message; result = value of var".
>     (when (nil? (value of the var called name))
>        (display (str name " is nil")))
>     (value of the var called name))
>
> so that
>
> (def x nil)
> (checkNil "x")
>
> should display: "x is nil" -- (display s) being a function that shows
> the string s somewhere.
>
> The question is, how do I express (value of the var called name)?
>
> I tried this: (var (symbol name))
> but that caused the exception: clojure.lang.PersistentList cannot be
> cast to clojure.lang.Symbol
>
> Is this possible?
>
> ----------------
> Another way to do it would be as follows.
>
> (defn checkNil [x]
>     "If the variable x is nil, display a message; result = value of
> x."
>     (when (nil? x)
>        (display (str (name of the var x) ) " is nil")))
>       x)
>
> so that
>
> (def x nil)
> (checkNil x)
>
> should display: "x is nil".
>
> In this case - how to do: (name of the var x)?
> >


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

Reply via email to