Re: 'switch-expression' and the 'type guard' unary operator demo
| From: | Rowan Tommins | Date: | Mon, 21 Oct 2019 09:08:46 +0000 |
| Subject: | Re: 'switch-expression' and the 'type guard' unary operator demo | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107592@lists.php.net to get a copy of this message | ||
Hi Kosit,
On Sun, 20 Oct 2019 at 16:20, Kosit Supanyo <webdevxp.com@gmail.com> wrote:
> You can recognize the difference of those by looking for
=>
> right? But
> the parser generator (bison) cannot do that in LR mode (it maybe can in GLR
> mode but I'm sure that would be unacceptable due to performance losses/more
> memory usage). So workarounds have to be used. I did it by simply return
> different tokens from the lexer base on previous token before
> switch.
>>
>>
Thanks for clarifying, I thought it might be something like that.
> * Different comparisons applied to the same variable/expression, e.g.
>> match($user->getScore()) { case <0 => foo(), case >100 => bar(), default
>> => baz() }
>
>
> I'd thought about that feature too. But since I also introduced type guard
> operator which uses < token it would have parser conflicts
> and simple
> workarounds cannot apply.
>
I'm not sure <foo> is the right syntax for type guards, but it's possible
we'd want a different syntax for new switch/match functionality anyway. For
instance, using a placeholder for the value being tested, like $$ (a common
choice in other languages is _, but that's a valid and commonly used
function name):
match ( $user->getScore() ) {
$$ < 0, $$ > 100 => foo(),
$$ < 10 => bar(),
default => baz()
}
You could also allow the placeholder in the opening clause to define the
same operation for each case:
match ( $user->getScore() < $$ ) {
0, default => foo(),
10 => bar(),
100 => baz()
}
Regards,
--
Rowan Tommins
[IMSoP]