Promises in D
Sebastiaan Koppe
mail at skoppe.eu
Thu Apr 8 11:13:55 UTC 2021
On Thursday, 8 April 2021 at 09:31:53 UTC, Vladimir Panteleev
wrote:
> I see, thanks! So, if I understand correctly - to put it in
> layman terms, senders/receivers is just a structured way to
> chain together callables, plus propagating errors (as with
> promises), plus cancellation. I understand that `setValue` just
> calls the next continuation with its argument (as opposed to
> storing the value somewhere as its name might imply), which
> means that the value may reside on the stack of the sender's
> start function, and remain valid only until `setValue` exits.
> The API is also somewhat similar, and I understand the main
> distinction is that starting execution is explicit (so, and the
> end of your `.then` chain, there must be a `.start()` call
> OSLT).
Yes, but be aware that the callee of .start() has the obligation
to keep the operational state alive until *after* one of the
three receiver's functions are called.
Often, instead of calling `.start` you would call `.sync_wait`,
or just return the sender itself (and have the parent take care
of it).
> I see how you could write a fiber-based executor/scheduler,
> but, I don't see how you could use these as a base for a
> synchronous fiber API like async/await. With delegates (and
> senders/receivers), there is a known finite lifetime of the
> value being propagated. With async/await, the value is obtained
> as the return value of `await`, which does not really provide a
> way to notify the value's source of when it is no longer needed.
Hmm, I see. But isn't that the limitation of async/await itself?
I suppose the solution would be to build refcounts on top of the
value, such that the promise hold a reference to the value
(slot), as well as any un-called continuations. Which would tie
the lifetime of the value to that of the promise and all its
continuations.
Ultimately this is all caused by the promise's design.
Specifically the fact that you can `.then` the same promise twice
and get the same value. Senders/Receivers don't have this. You
get the value/error/done exactly once. Calling start again is not
allowed.
More information about the Digitalmars-d
mailing list