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.
> ```php
> 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]