是否有一个Clojure编译时​​工具来检查一个记录或类型是否实际上实现了它声称的协议? [英] Is there a Clojure compile-time tool to check if a record or type actually implements a protocol it claims to?

查看:128
本文介绍了是否有一个Clojure编译时​​工具来检查一个记录或类型是否实际上实现了它声称的协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎Clojure编译器不会这样做默认: Clojure编译器检查记录和类型是否实现协议?

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

任何人,比如,Lein插件,

Any, say, Lein plugins that do this?

推荐答案

惊人的 core.typed 介绍了Clojure的可选类型系统,您可以在其官方网站

具体来说,你可以使用自己的 defprotocol 宏( from core.typed wiki ):

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

协议定义应该使用clojure.core.typed / defprotocol,其
语法让人联想到defprotocol并输入fn:

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)))

支持多态协议:

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


一次 ,您可以通过使用 lein类型检查的leiningen运行它。
明显的缺点是你必须注释你的代码。这是通过使用静态类型检查来增加代码的安全性所花费的成本。

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.

您也可能对函数 令? instance?

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

这篇关于是否有一个Clojure编译时​​工具来检查一个记录或类型是否实际上实现了它声称的协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆