Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 454
Rob N Avatar asked Jan 19 '26 15:01

Rob N


1 Answers

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)
like image 115
Carcigenicate Avatar answered Jan 22 '26 06:01

Carcigenicate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!