instance? tests whether a value is an instance of a specified Java class or interface, returning a boolean according to Java inheritance rules. section 15.2
Use instance? when the relevant question is whether a runtime value conforms to a Java type:
- A bigint is an instance of java.lang.Number. section 15.2
- An integer is an instance of java.lang.Comparable. section 15.2
- clojure.test accepts instance? as a predicate for assertions that are not equality checks. section 22.9
Checking classes and interfaces
(instance? java.lang.Number (bigint 1)) ;; true (instance? java.lang.Comparable 1) ;; true section 15.2
Using instance? in tests
(is (instance? Double (sqrt nil))) ;; Error: java.lang.NullPointerException section 22.9
