Joseph Yourine
Joseph Yourine

Reputation: 1331

Writing a multi-arity defn macro

I did not know how to phrase that but here is the problem :

(defmacro defbasic [val-fn]
  (let [;; Like str but keywords are str'd without :
        fn-name (fdat/->string "make-" val-fn)
        ;; This is a hashmap got from basic-builders
        ;; which is def
        options (get basic-builders val-fn)]
   `(defn ~fn-name
      ([]
       (~fn-name {}))
      ([descriptor]
       (->basic-operation ~options descriptor)))))

The function ->basic-operation works and is defined before. The intent of the macro is to create generic coercers like :

(defbasic :cat) ;; ==> defn a make-cat function 

I saw the clojure spec throws but I do not see what the problem is. I see the map and the right data.

I thought that maybe the problem was the fact that there is a list of arities ?

Thanks

Upvotes: 1

Views: 204

Answers (1)

Joseph Yourine
Joseph Yourine

Reputation: 1331

Ok I finally found the problem by tral-guess-error :

(defmacro defbasic [val-fn]
  (let [fn-name (symbol (fdat/->string "make-" val-fn))
        options (get basic-builders val-fn)]
   `(defn ~fn-name
      ([]
       (~fn-name {}))
      ([descriptor#]
       (->basic-operation ~options descriptor#)))))

I forgot two things :

  • make function name a symbol, which is not the same as a String and is not coerced to it automatically
  • generate a symbol for descriptor with #

It hope all of these would be documented in a single place, many learning content does not explicitely explain these symbols (or I missed it).

Upvotes: 3

Related Questions