开发者

Is there a way to get a collection of clojure special forms programatically?

Does something similar to this exist?:

(deftest fantasy
   (is (= ["let" "def" "ns" "etc."] clojure.core/special-chars)))开发者_StackOverflow社区


I don't think there's a good way to do it.

But here's a hack (run on a bleeding-edge 1.2 snapshot):

user> (map (comp name first) (seq clojure.lang.Compiler/specials))
("deftype*" "new" "quote" "&" "var" "set!" 
 "monitor-enter" "recur" "." "case*" "import*" 
 "reify*" "do" "fn*" "throw" "monitor-exit" "letfn*" 
 "finally" "let*" "loop*" "try" "catch" "if" "def")

This is relying on implementation details of the compiler. Please don't use this in production code.

Note that let is considered a special form, even though it's really a normal macro and let* is the actual special form. Same with fn and others. These are probably implementation details, subject to change in the future.

So if you really want a list of special forms, I'd suggest typing the list yourself. The list is short enough (everything listed on http://clojure.org/special_forms). Look at the source for clojure.core/special-form-anchor, there's a hard-coded list there to copy/paste. (Again probably an implementation detail not to be relied upon though.)

#{'. 'def 'do 'fn 'if 'let 'loop 'monitor-enter 'monitor-exit 'new 'quote 'recur 'set! 'throw 'try 'var}

And there's also this:

user> (clojure.core/special-symbol? 'if)
true
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜