Hi Robert

On Wed, Feb 14, 2024 at 1:29 AM Robert Landers <landers.rob...@gmail.com> wrote:
>
> I won't be the first to say this, at first glance, casting to null
> sounds silly, but short arrow functions must always return something,
> by design. That's when casting to null makes any sense at all (that I
> can think of): you want to write a succinct, short function but
> guarantee the result is discarded. Instead, if you really must use a
> short array function, you have to do something even weirder:
>
> EventLoop::repeat($pingInterval, fn() => $client->ping() ? null : null);

While (void) $client->ping() would solve your problem, it's not very
useful outside this scenario.

I think there are two issues you're implicitly referring to.

1. Arrow functions cannot be void, because they always return something.
2. Arrow functions cannot contain multiple statements.

As for the former, PHP actually had a similar issue for never closures
that was solved a while ago [1]. In the report I suggested that the
same could be done for void, by making void arrow functions evaluate
and drop the right hand side of =>, and always return nothing (i.e.
null). This would solve your issue, although probably mostly by
accident (because void functions return null, and your caller expects
exactly null). Regardless, I think this change would be useful, if
just to signal that the return value of an arrow function is not
intended to be used.

The latter would require some sort of block or grouped expression.
Short closures have been discussed extensively in the past, so I won't
get into that. There's also the comma operator in some languages like
C and JavaScript that evaluates a list of expressions and returns the
result of the last one, although probably not universally liked (i.e.
fn () => ($client->ping(), null)).

[1] 
https://github.com/php/php-src/commit/f957e3e7f17eac6e9195557f3fa79934f823fd38

Ilija

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to