Re: Inline switch as alternative to nested inline conditional
| From: | Kosit Supanyo | Date: | Wed, 16 Oct 2019 08:37:13 +0000 |
| Subject: | Re: Inline switch as alternative to nested inline conditional | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107556@lists.php.net to get a copy of this message | ||
I mean separation between cases not case conditions. Examples in your RFC
uses semicolon as case separator instead of comma.
<?php
$say = switch (date("w")) {
case 0 => "weekend!";
case 1, 2, 3, 4, 5 => "weekday :(";
case 6 => "weekend!";
};
echo "Today is {$say}";
I prefer comma because semicolon should only be used to separate statements
but switch expression is list of condition/expression pairs not statements.
So it should be:
<?php
$say = switch (date("w")) {
case 0 => "weekend!",
case 1, 2, 3, 4, 5 => "weekday :(",
case 6 => "weekend!",
};
echo "Today is {$say}";
You may think t's fine because Java uses semicolon but IMO Java had made a
mistake. (C# is semantically correct)
Cheers
On Wed, Oct 16, 2019 at 2:54 PM Michał Brzuchalski <
michal.brzuchalski@gmail.com> wrote:
> 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 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
>