2
votes

I'm trying to create a macro that makes a Clojure deftypeand which requires type hints to be generated. I currently have some test code:

(defmacro test-macro [n]
  (let [obj-sym (gensym "obj")
    p0 (with-meta 'p0 {:tag java.lang.Object})
    p1 (with-meta 'p1 {:tag java.lang.Integer/TYPE})
    r0 (with-meta 'remove {:tag java.lang.Boolean/TYPE})
    r1 (with-meta 'remove {:tag java.lang.Object})]
`(deftype ~n [~obj-sym]
   java.util.List
   (~r0 [_ ~p0] (.remove ~obj-sym ~p0))
   (~r1 [_ ~p1] (.remove ~obj-sym ~p1)))))

When it returns:

(test-macro test-it)
;clojure.lang.Compiler$CompilerException: java.lang.IllegalArgumentException: Must hint overloaded method: remove, ...

As a guide, it should produce something equivalent to:

(clojure.core/deftype ThisWorks [obj-5546]
  java.util.List
   (#^"boolean" remove [_ ^java.lang.Object p0-object] (.remove obj-5546 p0-object))
   (^{:tag "java.lang.Object"} remove [_ ^int p0-int] (.remove obj-5546 p0-int)))

It looks like I am type hinting the wrong thing, or the meta data isn't being passed. Other than a fix for the immediate problem, bonus points if you can help out with the more general "meta" problem: how to debug a macro which manipulates metadata as macroexpand isn't very useful here..

Thanks

1

1 Answers

5
votes

The :tag metadata is supposed to be a symbol, not a class object (since, after all, a symbol is all you can type as code in a non-macro situation: I can't embed the Class object int itself in my code!). So rather than Integer/TYPE, you just want 'int, and similarly for all your other typehints.