Re: Inline switch as alternative to nested inline conditional
| From: | Michał Brzuchalski | Date: | Wed, 16 Oct 2019 07:54:27 +0000 |
| Subject: | Re: Inline switch as alternative to nested inline conditional | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107555@lists.php.net to get a copy of this message | ||
Hi Kosit,
śr., 16 paź 2019 o 09:41 Kosit Supanyo <webdevxp.com@gmail.com> napisał(a):
> Hi Michal
>
> I'had the idea similar to your RFC but instead of using
switchñì¡
> _e)€ñaR:Q€ keyword I
> think introducing when keyword is better. (as I know a language
> that uses
> this keyword is Kotlin)
>
> $result = when ($v) {
> 'foo' => 1,
> 'bar' => 2,
> 'x', 'y', 'z' => 3,
> default => null,
> };
>
> Above you can see that when syntax is more semantic than
> switch. And
> it is easier to implement in parser because it has no statement/expression
> ambiguity.
>
> But this might not be preferred by BC camps because introducing a new
> keyword might break BC.
> And if switch is chosen I still think the syntax should be
> comma
> separated instead of semicolon separated for consistency with other list
> expressions (array/function call).
>
> $result = switch ($v) {
> case 'foo' => 1,
> case 'bar' => 2,
> case 'x', 'y', 'x' => 3,
> default => null,
> };
>
That's exactly my proposal with commas, see here:
https://wiki.php.net/rfc/switch-expression-and-statement-improvement#switch_expression_introduction
Unfortunately, this RFC needs more work cause it mixes switch statement
enhancement with comma-separated list syntax and of switch statement - I
need to split it into two RFC's
This is nice hearing that this idea has an interest.
As soon as the RFC will be split and finished I can start a discussion
thread.
Cheers,
Michał Brzuchalski