How do I check if a variable implements an interface in clojure?

社会主义新天地 提交于 2020-01-14 07:51:09

问题


So I have defined say a keyword:

(def a :hello)

how do I check that it implements the IFn interface?


回答1:


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

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

Quoting the documentation:

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

For example:

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

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)


来源:https://stackoverflow.com/questions/12170412/how-do-i-check-if-a-variable-implements-an-interface-in-clojure

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