本文介绍了如何检查变量是否在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>)

文档:

例如:

(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中实现了接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:48
查看更多