[Discussion] Thoughts on casting to null

From: Date: Wed, 14 Feb 2024 00:29:05 +0000
Subject: [Discussion] Thoughts on casting to null
Groups: php.internals 
Request: Send a blank email to internals+get-122365@lists.php.net to get a copy of this message
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

« previous php.internals (#122365) next »