小编mav*_*ozo的帖子

使用前向声明的宏时,args的数量错误

我有一个函数my-plus-in-macro,它包含一个名为my-macro的宏.但my-macro是在my-plus-macro定义之后声明的.我使用declare来避免无法解决的符号错误.

我在我的repl中输入这些代码.

(declare my-macro my-plus)


(defn my-plus-in-macro [x y]
    (my-macro (my-plus x y)))


(defmacro my-macro [my-fn]
    `~my-fn)


(defn my-plus [x y]
   (+ x y))
Run Code Online (Sandbox Code Playgroud)

然后,如果执行此操作,我希望得到3

(my-plus-in-macro 1 2)
Run Code Online (Sandbox Code Playgroud)

但我得到了,

ArityException Wrong number of args (1) passed to: user$my-macro  clojure.lang.AFn.throwArity (AFn.java:437)
Run Code Online (Sandbox Code Playgroud)

但是,如果我在repl中再次执行此my-plus-in-macro定义

(defn my-plus-in-macro [x y]
    (my-macro (my-plus x y)))
Run Code Online (Sandbox Code Playgroud)

然后我执行这个(my-plus-in-macro 1 2)

正是我所期待的,3

我第一次执行(my-plus-in-macro 1 2)会发生什么?

不知怎的,当我确定我加在宏首次,Clojure中看到我的宏符号,但它不知道的还以为我打算用它作为一个宏的名称.

虽然我在定义my-plus-in-macro后立即将my-macro定义为符号,但是clojure仍然不知道 …

clojure

4
推荐指数
1
解决办法
581
查看次数

标签 统计

clojure ×1