问题描述:

In Clojure, for quote I can use either the tick character or quote:

user=> '(+ 2 2)

(+ 2 2)

user=> (quote (+ 2 2))

(+ 2 2)

However for syntax-quote I can seemingly only use the backtick operator:

user=> `(+ 2 2)

(clojure.core/+ 2 2)

user=> (syntax-quote (+ 2 2))

CompilerException java.lang.RuntimeException: Unable to resolve symbol: syntax-quote in this context, compiling:(NO_SOURCE_PATH:1:1)

Is there a way to invoke syntax-quote without using the backtick character?

网友答案:

You can use clojure.tools.reader

(require '[clojure.tools.reader :as r])
-> nil

(r/syntax-quote +)
-> clojure.core/+
网友答案:

This questions has largely been answered here. Since then, the clojure-in-clojure analyzer was created so you can get something somewhat similar to a syntax quote using:

(use '[clojure.tools.analyzer.passes.jvm.emit-form :only [emit-form]]
 '[clojure.tools.analyzer.jvm :only [analyze]])

(emit-form (analyze '(+ 2 2)))
;-> (clojure.lang.Numbers/add 2 2)

As you can see, the function is actually resolved right down to the Java code that will execute rather than just resolving the namespace of the symbol.

相关阅读:
Top