Fork me on GitHub
#core-async
<
2019-12-20
>
mpenet15:12:36

just curious: why isn't a promise-chan auto-closed when it holds a "realized" value

mpenet15:12:20

since it will return the delivered value on takes just the same

mpenet15:12:13

> * After a put, all pending takes are notified, and any subsequent takes return the put value immediately > * After a close (and no put), all pending takes are notified with nil, and any subsequent takes return nil immediately

mpenet15:12:31

I guess it's to allow this nil broadcasting

mpenet15:12:52

ah wait no, it's only if no put happened

mpenet15:12:22

in my case in particular I was surprised by the put! return value on undelivered/already delivered/closed promise-chan

mpenet15:12:16

In short:

(-> (doto (async/promise-chan)
          (async/>!! :foo)
          (async/close!))
    (async/put! :bar))
false

vs

(-> (doto (async/promise-chan)
          (async/>!! :foo))
    (async/put! :bar))
true

Alex Miller (Clojure team)15:12:34

promise-chans are inherently multi listener (as all channels). there are two cases - a value is delivered, or the chan is closed. All pending listeners receive either the value, or nil.

fmjrey16:12:26

The question is more about puting a value to a promise-chan that already has a value

mpenet16:12:32

right now there's a 3rd case, the value is delivered and the chan is close!d. I get why we have close! for an "empty" promise-chan, it's just a bit weird when it's already realized

mpenet16:12:14

@fmjrey I am fine with that, it's for convenience and a way to know if it's already closed

mpenet16:12:47

same as when you put into a normal closed chan, it will just drop it and return nil

mpenet16:12:27

that's what I quoted before

fmjrey16:12:03

I know, just making it easier for whoever else is following

mpenet16:12:58

it's possible to golf around having autoclosing promise-chan via an xform

mpenet16:12:03

but yuk 🙂

mpenet16:12:00

(async/promise-chan (take 1))

mpenet16:12:10

(-> (doto (async/promise-chan (take 1))
      (async/>!! :foo))
    (async/put! :bar))
 => false