Rob N
Rob N

Reputation: 16419

In Clojure, how to check if an object is a core.async channel?

There is a function chan to create a channel. But I don't see a chan?. How would I write a predicate chan? that returns true for objects created by chan?

I'm asking about both Clojure and ClojureScript.

Upvotes: 3

Views: 1238

Answers (1)

Carcigenicate
Carcigenicate

Reputation: 45750

Since channels are implemented as:

(deftype ManyToManyChannel [^LinkedList takes ^LinkedList puts ^Queue buf closed ^Lock mutex add!]
   ...)

You can just check if it's an instance of ManyToManyChannel:

(import [clojure.core.async.impl.channels ManyToManyChannel])

(instance? ManyToManyChannel obj)

Or, if you care more about the protocols rather than the type itself, you can check if the object satisfies? one the the protocols:

(satisfies? clojure.core.async.impl.protocols/WritePort
            obj)

Upvotes: 9

Related Questions