Hi Rowan 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.
I've decided to change the syntax to `(: foo)`. It may look weird at frist but it is able explain why I think this syntax is more appropriate. Because PHP already uses `:` to specify return type of functions so `(: foo)` syntax can be seen as a way to specifying return type of expressions. And it can be put into the same visual category of cast operators. $a = (int)$b; // filter $c = (: int)$d; // guard This syntax choice may not satisfy everyone's taste but at least it is explainable. So now there's 2 choices `<foo>` and `(: foo)`. And more choices are welcome from anyone who think type guard should be a new feature. 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): About that, I'm working on a different approach which I would like to show you after I finished the next demo :) Regards On Mon, Oct 21, 2019 at 8:52 PM Rowan Tommins <rowan.coll...@gmail.com> wrote: > Hi Kosit, > > On Sun, 20 Oct 2019 at 16:20, Kosit Supanyo <webdevxp....@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] >