Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I extend Clojure's IFn using extend-type?

I saw David Nolen's talk on ClojureScript where he makes extends IFn to Regexp and so that we can call a regex literal as a fuinction on a string to check for matches.

I tried something similar with Strings in regular Clojure, but I get this exception. I see that IFn is an interface written in Java but how does the implementation work in CLojrue front.

(extend-type java.lang.String
  #_=>    clojure.lang.IFn
  #_=>   (-invoke
  #_=>    ([this index]
  #_=>     (get (seq this) index))))
IllegalArgumentException interface clojure.lang.IFn is not a protocol  clojure.core/extend (core_deftype.clj:742)
like image 204
Amogh Talpallikar Avatar asked Jul 26 '14 20:07

Amogh Talpallikar


1 Answers

The exception should give you a hint. IFn is not a protocol in Clojure. It is in ClojureScript. You can extend protocols, implement Java interfaces, and subclass classes. In this particular case, you cannot even subclass java.lang.String because it's declared final.

like image 53
Diego Basch Avatar answered Nov 07 '22 12:11

Diego Basch