问题
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.
来源:https://stackoverflow.com/questions/32025365/does-the-clojure-compiler-check-if-records-and-types-implement-protocols