Does the Clojure compiler check if records and typ

2019-08-13 15:36发布

问题:

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.

回答1:

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.