Re: Inline switch as alternative to nested inline conditional
| From: | Kosit Supanyo | Date: | Wed, 16 Oct 2019 07:41:02 +0000 |
| Subject: | Re: Inline switch as alternative to nested inline conditional | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107554@lists.php.net to get a copy of this message | ||
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,
};
Cheers
On Wed, Oct 16, 2019 at 10:33 AM Michał Brzuchalski <
michal.brzuchalski@gmail.com> wrote:
> Hi David,
>
> I'm interested in this feature this is called switch expression and is on
> my list of RFC to process after Object Initializer RFC.
>
> I have an RFC in draft for that and planning to finish it soon together
> with some syntax optimisations - you can see it at
>
> https://wiki.php.net/rfc/switch-expression-and-statement-improvement
>
> Cheers,
> Michał Brzuchalski
>
> śr., 16 paź 2019, 03:46 użytkownik David Rodrigues <david.proweb@gmail.com
> >
> napisał:
>
> > Hello. I like to suggests a discussion about a FR to make possible to
> > inline switch, as an alternative to nested inline conditionals.
> >
> > $value = switch (expr()) {
> > case A1: return A2;
> > case B1: return B2;
> > default: return C;
> > }
> >
> > Instead of:
> >
> > $expr = expr();
> > $value = $expr == A1 ? A2 : ( $expr == B1 ? B2 : C );
> >
> > Just a discussion to check what do you think.
> >
> > --
> > David Rodrigues
> >
>