如何检查变量是否在clojure中实现了接口? [英] How do I check if a variable implements an interface in clojure?

查看:45
本文介绍了如何检查变量是否在clojure中实现了接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我定义了一个关键字:

So I have defined say a keyword:

(def a :hello)

如何检查它是否实现了IFn接口?

how do I check that it implements the IFn interface?

推荐答案

在一般情况下,可以使用 实例? 谓词:

For the general case, you can use the instance? predicate:

(instance? <class-or-interface> <object>)

文档:


(实例?cx)计算x并测试它是否为
类的实例c。返回true或false。

(instance? c x) evaluates x and tests if it is an instance of the class c. Returns true or false.

例如:

(instance? java.lang.String "test")
> true

(instance? java.io.Serializable "test")
> true

对于问题中的代码,请执行以下操作:

For the code in the question, do something like this:

(instance? package.of.IFn a)

或者,如评论中所指出的,对于询问 a 是否为 IFn的实例的特定情况/ code>这将起作用:

Or, as has been pointed out in the comments, for the very specific case of asking if a is an instance of IFn this will work:

(ifn? a)

这篇关于如何检查变量是否在clojure中实现了接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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