Hello Internals,

Today, I was working with a library (ReVolt) that throws if a callback
returns something other than null (why it doesn't ignore the value, I
don't know, maybe I'll file an issue). I discovered the topic of this
email after trying to be simple:

EventLoop::repeat($pingInterval, $client->ping(...));

Where `ping()` will return a bool which triggers an error since it
returns something. I thought to myself, cool, I'll just cast the
result to null and everything will be fine:

EventLoop::repeat($pingInterval, fn() => (null) $client->ping());

However, this isn't allowed and fails with a parse error.

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);

I assume casting to null was discussed previously (at some point,
though I didn't see anything), but what are your thoughts? Is this
something that even makes sense? I'll admit, I've never tried casting
to null before, but I had assumed it would "just work" and was
slightly surprised that it didn't.

Robert Landers
Software Engineer
Utrecht NL

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

Reply via email to