问题
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