Convert return to expression like throw

From: Date: Fri, 18 Mar 2022 10:18:53 +0000
Subject: Convert return to expression like throw
Groups: php.internals 
Request: Send a blank email to internals+get-117369@lists.php.net to get a copy of this message
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

« previous php.internals (#117369) next »