Fork me on GitHub

I'm reproducing the examples of Tim Baldridge's O'Reilly course:

(def c (chan 24))

  (loop [i 0] 
     (println "Putting: " i) 
     (>! c i) 
     (recur (inc i))))

(<!! c) 
This code demonstrates back pressure. Whenever I take a value from the channel with
(<!! c)
a new value is put onto the channel by the loop. I closed the channel with
(close! c)
to see what happens. Strangely, the loop no longer blocked and started printing thousands of "Putting: ..." lines. How can this be explained? I would have expected an error.


> puts a val into port. nil values are not allowed. Must be called > inside a (go ...) block. Will park if no buffer space is available. > Returns true unless port is already closed. if the put doesn't succeed, it returns false. You are attempting to put onto a closed channel which immediately returns false and then you loop again


(docstring from >!)


using the >!! version but demonstrates a similarity:

(let [closed-chan (doto (a/chan) (a/close!))]
  (a/>!! closed-chan 3))
=> false


i believe your expectation was that this would block forever as it was unable to put onto the closed channel


I see. Thanks!