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

This is only part of a thread. view whole thread
  110267
May 23, 2020 07:30 davey@php.net (Davey Shafik)
On Fri, May 22, 2020 at 4:09 AM Ilija Tovilo ilija@gmail.com> wrote:

> Hi internals > > I'd like to announce the match expression v2 RFC: > https://wiki.php.net/rfc/match_expression_v2 > > The goal of the new draft is to be as simple and uncontroversial as > possible. It differs from v1 in the following ways: > > * Blocks were removed > * Secondary votes were removed > * optional semicolon > * omitting `(true)` > * Unimportant details were removed (e.g. examples for future scope) > > You can look at the diff here: > https://github.com/iluuu1994/match-expression-rfc/pull/8/files > > I will also leave the discussion period open for longer as that too > was one of the primary criticisms. > > As mentioned by Kalle: > > > Resurrecting rejected RFCs have a "cooldown" of 6 months: > > https://wiki.php.net/rfc/voting#resurrecting_rejected_proposals > > That is, unless: > > > The author(s) make substantial changes to the proposal. While it's > > impossible to put clear definitions on what constitutes 'substantial' > > changes, they should be material enough so that they'll significantly > > affect the outcome of another vote. > > Given that many people have said without blocks they'd vote yes I'd > say this is the case here. Let me know if you don't agree. > > Ilija >
With these simplifications, I'm not entirely sure there is enough value here if the Conditional Return, Break, and Continue Statements RFC is passed. This could be written with the slightly more ugly, but serviceable: So this: $statement = match ($this->lexer->lookahead['type']) { Lexer::T_SELECT => $this->SelectStatement(), Lexer::T_UPDATE => $this->UpdateStatement(), Lexer::T_DELETE => $this->DeleteStatement(), default => $this->syntaxError('SELECT, UPDATE or DELETE'), }; could be expressed as: echo (function($match) { return $this->SelectStatement() if ($match === Lexer::T_SELECT); return $this->UpdateStatement() if ($match === Lexer::T_UPDATE); return $this->DeleteStatement() if ($match === Lexer::T_DELETE); return $this->syntaxError('SELECT, UPDATE or DELETE'); })($this->lexer->lookahead['type']); 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. Having said that… match is undeniably prettier. 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. - Davey
  110293
May 28, 2020 16:12 tovilo.ilija@gmail.com (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