Re: PHP True Async RFC - Stage 2

From: Date: Thu, 20 Mar 2025 21:41:25 +0000
Subject: Re: PHP True Async RFC - Stage 2
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 20/03/2025 11:01, Edmond Dantes wrote:
Specifically, what is the use case where syntax #2, "spawn function_call" is not good enough, leading us to add a special case into the grammar.
Additional parentheses around + parentheses after. That is, (closure)(). The goal is to get rid of this construct.
Again, that's the *how*, not the *why*. We only need to "get rid of" the parentheses if there's some reason to type them in the first place.
A safer approach would be to implement only syntax 2 and consider the alternative option only if user feedback suggests it's needed. Sounds like a solution without drawbacks...
This is pretty much where my mind is going - if we can't articulate an actual reason why "define an inline closure and pass it to spawn" is so common it requires special implementation, then let's keep it simple.
spawn fn() => [file_get_content(), file_get_content(), file_get_content()]
At this point, I haven't been able to come up with examples where such a closure would actually be convenient.
Maybe this use case will emerge later.
I was thinking of something like this maybe: $contentFuture = spawn ( fn() => file_exists($filename) ? file_get_contents($filename) : '!! no such file !!' )(); Or even: spawn ( fn() => do_something( fetch_something($input) ) )(); Which looks like it would be equivalent to this, but isn't: spawn do_something( fetch_something($input) ); (It calls fetch_something() inside the coroutine, rather than outside it.) If anything, this seems like a better candidate for a shorthand than the full closure syntax, because we already have the rules for automatic capture and automatic return available to reuse. Maybe it could be as short as this: spawn => do_something( fetch_something($input) ) ); From longest to shortest: spawn ( function() use($input) { do_something( fetch_something($input) ); } )(); spawn function use($input) { do_something( fetch_something($input) ); }; spawn ( fn() => do_something( fetch_something($input) ) )(); spawn => do_something( fetch_something($input) ) ); Or, if we get Pipe syntax, it could end up like this: spawn => $input |> fetch_something(...) |> do_something(...); Again, though, this could easily be added later when a need becomes visible, as long as we don't do something weird now that closes the door on it. I suggest we leave this sub-thread here; there's plenty of other things to discuss. :) -- Rowan Tommins [IMSoP]

Thread (59 messages)

« previous php.internals (#126881) next »