Question

I'd like to test whether a collection is transient. I tried the following function:

(defn transient? [coll]
  (extends? clojure.lang.ITransientCollection (type coll)))

But it doesn't work:

user=> (transient? {})
NullPointerException   clojure.core/implements? (core_deftype.clj:512)

user=> (transient? (transient {}))
NullPointerException   clojure.core/implements? (core_deftype.clj:512)

The documentation of extends? doesn't mention that it would work with Java interfaces anyway. I was just hoping they'd work. Is there any other way to determine if a collection is transient?

I'm using Clojure 1.5.1.

Was it helpful?

Solution

You need to use instance? not extends?.

extends? asks whether a type extends a protocol. ITransientCollection is a Java interface, not a protocol, so instance? is the tool for that.

user=> (instance? clojure.lang.ITransientCollection (transient {}))
true

user=> (instance? clojure.lang.ITransientCollection {})
false
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top