On Wed, Oct 15, 2025, at 09:53, Edmond Dantes wrote:
> Hello.
> 
> > Why doesn’t scope implement Awaitable?
> 
> Let’s say there’s a programmer named *John* who is writing a library,
> and he has a function that calls an external handler.
> Programmer *Robert* wrote the `externalHandler`.
> 
> ```php
> function processData(string $url, callable $externalHandler): void
> {
>    ...
>     $externalHandler($result);
> }
> ```
> 
> John knows which contracts are inside processData, but he knows
> nothing about `$externalHandler`.
> From the perspective of `processData`, `$externalHandler` acts as a
> **black box**.
> If John uses `await()` on that black box, it may lead to an infinite wait.
> 
> There are two solutions to this problem:
> 
> 1. Delegate full responsibility for the program’s behavior to
> *Robert*, meaning to `$externalHandler`
> 2. Establish a limiting contract
> 
> Therefore, if a `Scope` needs to be awaited, it can only be done
> together with a cancellation token.
> 
> In real-world scenarios, awaiting a `Scope` during normal execution
> makes no sense, because you have a `cancellation policy`.
> This means that at any necessary moment you can dispose() the `Scope`
> and thus interrupt the execution of tasks inside the black box.
> 
> For the `TaskGroup` pattern, which exists in the third version of the
> RFC, awaiting is a relatively safe operation, because in this case we
> assume that the code is written by someone who has direct access to
> the agreements and bears full responsibility for any errors.
> 
> So, `Scope` is intended for components where responsibility is shared
> between different programmers, while `TaskGroup` should be used when
> working with a clearly defined set of coroutines.
> 

I don’t get it. What does different programmers working on a program have to do 
with whether or not scopes implements Awaitable? Scope has an await method, it 
should be Awaitable. await() takes a cancellation and thus anything Awaitable 
can be cancelled at any time. I don’t see why scope is special in that regard.

> If John uses `await()` on that black box, it may lead to an infinite wait.

This is true of any software or code. Knowing whether or not something will 
ever complete is called The Halting Problem. It is unsolvable, in the general 
sense. You can await() a read of an infinite file, or a remote file that will 
take 5y to read because it is being read at 1 bps. Your clock can fry on your 
motherboard, preventing timeouts from ever firing. Your disk can die mid-read, 
preventing it from ever sending you any data. There is so much that can go 
wrong. To say that something that has an await method isn’t Awaitable because 
it may never return is true for ALL Awaitable tasks as well. It isn’t special. 

— Rob

Reply via email to