clojureclojure-protocol

Does the Clojure compiler check if records and types implement protocols?


Is the Clojure compiler meant to check if a record or type that says it instantiates a protocol actually implements the methods listed in it?

I'm trying this out now and so far, it doesn't seem to.


Solution

  • A record can implement a protocol without implementing any of its methods:

    (defprotocol Structure
      (weight [this])
      (balanced? [this]))
    
    (defrecord Mobile []
      Structure
      )
    

    ... is accepted.

    If you try to use a non-existent method:

    (balanced? (Mobile.))
    
    ;java.lang.AbstractMethodError: user.Mobile.balanced_QMARK_()Ljava/lang/Object;
    

    As usual, type errors are found at run time.