Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure : Determine if a variable is declared

How can I test wether a variable has been declared or assigned (i.e. check if "a" is defined, when I expect a program to call some code like this (def a (create-a)) ?

And related --- how does the answer to this question relate to the problem of resolving a symbol (i.e. a function) which has been declared ? Clojure: determine if a function exists

It seems like a defined variable should be checkable in the same sense that a defined function is, but I'm finding that the solution for determining if a function exists is not sufficient for determining wether a variable exists.

Some context : I'm writing unit tests for a multideveloper project, and want to make sure that the test data, and the methods in different classes have been defined. Since there is no good IDE support for clojure, it seems to me that, given its loose structure, it is good to test method names and variable names existence before testing their outputs / content.

like image 350
jayunit100 Avatar asked Oct 19 '11 22:10

jayunit100


1 Answers

You can use resolve to see if the variable was bound/defined:

(resolve 'meaning)
nil

(def meaning 42)
#'user/meaning

(resolve 'meaning)
#'user/meaning

or you can boolean check it, if you need true/false:

(boolean (resolve 'meaning))
true
like image 79
tolitius Avatar answered Oct 12 '22 13:10

tolitius