(rontolisp) docs
← Numbers and predicates

instance?

(instance? Class x)

true when x is an instance of the class, like the oracle's isInstance. A class or interface answers for every kind of value whose oracle class is or implements it: java.util.List for a vector, a list or a lazy seq, java.util.Map for a map or a record, clojure.lang.IFn for a function, keyword, symbol, map, set, vector or var, Comparable for a string, number, keyword or vector, java.io.Writer for *out*. Object is every value but nil. A known record/deftype name tests the dispatch tag. A protocol's interface (user.P, the namespace and name munged like the oracle's: my_app.core.my_p) is true of a record, deftype or reify whose body names the protocol, with methods or none; an extend-type or extend-protocol target is not (satisfies? is). A clojure.lang interface a body implements (reify: Counted, IFn ...) is true of the value, and so is each of its supers (Counted of an Indexed). A throwable class (Exception, IllegalArgumentException, clojure.lang.ExceptionInfo, a dotted or imported one) tests an exception or a runtime error by its class -- the class class answers or a subclass of it. A clojure.java.io value answers by its class and supers on every backend, so (instance? java.io.File (java.io.File. "x")) and (instance? java.io.Closeable (clojure.java.io/input-stream f)) are true. On the interpreter and the JVM a host object answers by its host class, so (instance? java.util.RandomAccess (java.util.ArrayList.)) is true; a class no value here has (Integer) answers false, and a name no class has is unknown name.

A class held in a local or a var, or computed ((def t (type (->R))), (instance? (class x) y)), is tested when the call runs: a class class or type answered is true of a value whose class is it and of an exception or runtime error of a subclass; a class spelled as a value ((def s String)) asks the host on the interpreter and the JVM. nil throws a NullPointerException and any other value that is no class a ClassCastException, like the oracle. (.isInstance c x) of a class class or .getClass answered is (instance? c x).