Re: Return When
| From: | Anton Smirnov | Date: | Tue, 26 Nov 2024 17:38:32 +0000 |
| Subject: | Re: Return When | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-126061@lists.php.net to get a copy of this message | ||
On 26/11/2024 18:10, Volodymyr Volynets wrote:
I have an idea which will save a lot of code. I am proposing to add": return when [condition], [return value]; This construct will remove a lot of ifs statements after method calls. For example: $result = Class->method(); if (!$result['success']) {Is there a language that uses a similar syntax? I don't find it readable. It also uses a new keyword (when) when you can reuse if I'd say, also comma does not look like a good separator here. Some alternatives could be: Perl&Ruby-like postfix if:return $result;} This becomes: return when !$result['success'], $result; Any thoughts?
return $result if (!$result['success']);or making return an expression that will allow us to do like this
$result['success'] ?: return false;and
match ($something) {
123 => do_stuff(),
default => return false,
}
but they both can open an endless can of worms I'd say so I agree with Christoph that the current syntax is good enough
Anton