Re: [PHP-DEV] [RFC][DISCUSSION] Match expression v2

This is only part of a thread. view whole thread
May 28, 2020 16:12 (Ilija Tovilo)
Hi Davey

> I mean, ultimately, it's equivalent to: > > echo (function($match) { > if ($match === Lexer::T_SELECT) { > return $this->SelectStatement() > } > if ($match === Lexer::T_UPDATE) { > return $this->UpdateStatement() > } > if ($match === Lexer::T_DELETE) { > return $this->SelectStatement() > } > return $this->syntaxError('SELECT, UPDATE or DELETE'); > })($this->lexer->lookahead['type']); > > The main selling point for match is that it's more concise, I'm not convinced that the return if variant is much less concise. If you want to match more than one thing, use the OR (double pipe) operator in the if condition.
There is no ultimate right or wrong here. For myself, there's a lot of cognitive overhead reading the code above. For others that might not be the case. There are also some other benefits of using match, like the jumptable optimization and a smaller risk of making mistakes.
> If we are doing this however, I'd prefer to implement Go's switch block, named match because we already have a switch. It's strict, but flexible.
Since most people expressed a desire for the match expression with no blocks I wanted to introduce the expression first. After 6 months of using match opinions on blocks might change, or they might not. If we do introduce blocks match should be able to do pretty much everything Go's switch can. Ilija