Is there a Clojure compile-time tool to check if a record or type actually implements a protocol it claims to?

北慕城南 提交于 2019-12-08 18:37:55

问题


Seems the Clojure compiler doesn't do this by default : Does the Clojure compiler check if records and types implement protocols?

Any, say, Lein plugins that do this?


回答1:


The amazing core.typed introduces "an optional type system for Clojure", as you can see on their official website.

Specifically you may want to use their own defprotocol macro (from core.typed wiki) :

Protocol definitions should use clojure.core.typed/defprotocol whose syntax is reminiscent of defprotocol and typed fn:

(defprotocol IUnifyWithLVar
  (unify-with-lvar [v u :- LVar s :- ISubstitutions] :- (U ISubstitutions Fail)))

Polymorphic protocols are supported:

(defprotocol [a b] Lens
   (-fetch [l x :- a] :- b)
   (-putback [l x :- a v :- b] :- a))

Once installed, you run it via leiningen with lein typed check. The obvious downside is that you have to annotate your code. This is the cost to pay to increase the safety of your code by using static type checking.

You may also be interested by the functions satisfies?, and instance?.



来源:https://stackoverflow.com/questions/32027613/is-there-a-clojure-compile-time-tool-to-check-if-a-record-or-type-actually-imple

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