Hello,

We remember the old days, when we had to do:

function getUnreadDocuments(): int {
$user = Auth::user();
if (is_null($user)) return 0;
$documents = $user->getDocuments();
if (is_null($documents)) return 0;
// actual logic of the function
return 42;
}

And it was already really nice to do:

function getUnreadDocuments(): int {
$documents = Auth::user()?->getDocuments();
if (is_null($documents)) return;
// actual logic of the function
return 42;
}

But I think it could be written even more concise with just reusing
existing syntax elements:

function getUnreadDocuments(): int {
$documents = Auth::user()?->getDocuments() ?? return 0;
// actual logic of the function
return 42;
}

This is exactly the type of change that PHP 8.0 introduced for throw (
https://wiki.php.net/rfc/throw_expression) and to me it feels very natural
to write code that has this change also made for the return statement. In
the example above, depending on developer preference, the same result could
be achieved with " || return 0;" instead of the ?? operator.

Was there any previous discussion about this and was there maybe a good
reason it hasn't been implemented? I couldn't find anything when searching.

What are your thoughts on this?

-Florian

Reply via email to