10

In Clojure core.async, are channels and ports the same thing? If not what's the difference? In watching the video Timothy Baldridge - Core.Async, he creates a channel

(def c (chan))

Then later

(<!! c)

c is channel yet the docs for <!! state (emphasis added)

Usage: (<!! port) takes a val from port. Will return nil if closed. Will block if nothing is available.

It's not clear looking at the core.async docs.

1 Answer 1

14

Yes, chans are ports.

port is in the name of the protocol that these implement

(defprotocol ReadPort
  (take! [port fn1-handler] "derefable val if taken, nil if take was enqueued"))

which is used by impl/take in:

(defn <!!
  "takes a val from port. Will return nil if closed. Will block if nothing is available."
  [port]
  (let [p (promise)
        ret (impl/take! port (fn-handler (fn [v] (deliver p v))))]
    (if ret
      @ret
      (deref p))))

and the name port is used very consistently throughout async.clj. Conceptually this is useful because not everything that core.async works on will be a channel. other things can implement ReadPort and WritePort and therefore play nicely with core.async.

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer, while well-written, does not explain why I need to know the difference between a chan and a port. The documentation shows <! being called in a go-block, but not <!!. Why?
you only need to know the difference if you are adding a new datatype that implements the port interfaces to allow it to send and receive data from other things in the core.async ecosystem. Chans are ports, and other things are as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.