2012-09-27 16 views
6

Estoy confundido en cuanto a las diferencias entre usar with-meta y la macro del lector ^.with-meta vs^{} - Clojure

Acople metadatos demasiado bazsymbol utilizando el lector macro

user=> (def ^{:foo "bar"} baz {:my "value"}) 
#'user/baz 

tirón hacia fuera

user=> (meta #'baz) 
{:foo "bar", :ns #<Namespace user>, :name baz, :line 1, :file "NO_SOURCE_PATH"} 

Una con with-meta

user=> (def (with-meta 'baz2 {:foo "bar"}) {:my "value"}) 
CompilerException java.lang.RuntimeException: First argument to def must be a Symbol, compiling:(NO_SOURCE_PATH:1) 
embargo

...

user=> (class (with-meta 'baz2 {:foo "bar"})) 
clojure.lang.Symbol 

puedo adjuntarlo al valor

user=> (def baz2 (with-meta {:my "value"} {:foo "bar"}) 
#'user/baz2 

pero no es el mismo

user=> (meta baz2) 
{:foo "bar"} 

user=> (meta #'baz2) 
{:ns #<Namespace user>, :name baz2, :line 1, :file "NO_SOURCE_PATH"} 

alguien puede explicar esto?

Respuesta

5

def es un special form. Aunque with-meta devuelve un símbolo, el compilador Clojure no (no puede) saber eso. Ve una función.

user=> (def (symbol blah) "blah") 
CompilerException java.lang.RuntimeException: First argument to def must be a Symbol, compiling:(NO_SOURCE_PATH:1)