Difference between Symbols and Vars in Clojure

前端 未结 3 484
野的像风
野的像风 2020-11-30 22:37

I\'m always a bit confused about Symbols and Vars in Clojure. For example, is it safe to say that + is a symbol which is used to denote a var, and this var points to a value

3条回答
  •  感动是毒
    2020-11-30 23:12

    See the documentation for namespaces:

    Namespaces are mappings from simple (unqualified) symbols to Vars and/or Classes. Vars can be interned in a namespace, using def or any of its variants, in which case they have a simple symbol for a name and a reference to their containing namespace, and the namespace maps that symbol to the same var. A namespace can also contain mappings from symbols to vars interned in other namespaces by using refer or use, or from symbols to Class objects by using import.

    So basically your steps 1 and 2 are unified: the namespaces are the symbol tables.

    And regarding step 3: I like the definition for variables that they're combinations of names of values. The symbol is the variable's name, and evaluating it will result in its value.

提交回复
热议问题