Clojure编译器检查记录和类型是否实现协议? [英] Does the Clojure compiler check if records and types implement protocols?

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

问题描述

Clojure编译器是否意味着检查一个记录或类型是否实例化一个协议实际上实现了其中列出的方法?

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?

我试试这个

推荐答案

记录可以实现协议而不实现其任何方法:

A record can implement a protocol without implementing any of its methods:

(defprotocol Structure
  (weight [this])
  (balanced? [this]))

(defrecord Mobile []
  Structure
  )

...被接受。

如果您尝试使用不存在的方法:

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.

这篇关于Clojure编译器检查记录和类型是否实现协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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