Wrong number of arguments when using declared macro ahead

I have a my-plus-in-macro function that contains a macro called my-macro. But my-macro is declared after the definition of my-plus-macro. I use declare to avoid incapable of fixing a character error.

I type these codes in my cue.

(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))

Then I expect to get 3 if I execute this

(my-plus-in-macro 1 2)

but instead i get

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

But, if I again fulfilled this definition of my-plus-in-macro in repl

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

then I am doing this (my-plus-in-macro 1 2)

What I expect 3

What happens on first run (my-plus-in-macro 1 2)?

Somehow, when I first define my-plus-in-macro, clojure sees the my-macro symbol, but it still does not know that I intend to use it as a name for the macro.

my-macro my-plus-in-macro, clojure , my-macro . ArityException.

- , my-plus-in-macro , clojure , my-macro - , .

, , my-plus-in-macro. , ?

@xsc, . my-macro "my-plus-in-macro", "" , : form : env

(declare my-macro my-plus)


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


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


(defn my-plus [x y]
   (+ x y))


(my-plus-in-macro 1 2)
3

-0

@xsc , declare . , .

(declare my-plus)

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

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

(defn my-plus [x y]
   (+ x y))

(my-plus-in-macro 1 2)
3
+4
1

, , / , , . declare, , , , .

: , my-macro, , . - ( :macro, true), , .

, "" , &form &env, :

(declare my-macro)

(defn my-function
  [x]
  (my-macro :form :env x))

(defmacro my-macro
  [x]
  (vector &form &env x))

(my-function (+ 1 2)) ;; => [:form :env 3]

, (+ 1 2) "", .

, :macro, , , , var -:

(declare ^:macro my-macro)

(defn my-function
  [x]
  (my-macro x))
;; => IllegalStateException: Attempting to call unbound fn: #'user/my-macro ...

TL; DR: , , /, , .

+9

Source: https://habr.com/ru/post/1533319/


All Articles