Do Clojure Protocols Support Type Hints?

Viewed 153

Do Clojure Protocols support type hints so that the same method name with the same parameters (except for the type hint) can be overloaded like in statically typed languages?

e.g.,

(defprotocol IFoo
  (get-val [this ^String astring])
  (get-val [this ^AClass aclass]))

If not, can I implement this, or do I have to rename the methods?

1 Answers

Afaik, you can't do this with defprotocol, still it is possible with definterface:

(definterface Intf
  (^String add [^String s])
  (^long add [^long l]))

(defrecord Rec [^long data]
  Intf
  (^String add [self ^String s] (str data s))
  (^long add [self ^long l] (+ data l)))

user> (let [r (Rec. 101)]
        [(.add r "asd")
         (.add r 200)])

;;=> ["101asd" 301]
Related