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

爱⌒轻易说出口 提交于 2019-12-13 19:36:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!