Proposal-pattern-matching: Expression vs statement

Created on 22 Jun 2020  路  14Comments  路  Source: tc39/proposal-pattern-matching

This point was deemed to be undecided in #164 so I've moved that discussion here. Should the right hand side of a when clause be an expression or a statement list?

Most helpful comment

I personally see no use in pattern matching as statements. All of the compelling use cases are using the match statement as an expression.

All 14 comments

https://github.com/tc39/proposal-pattern-matching/issues/164#issuecomment-647674912

If the RHS of the match is a statement list, [...] the overarching match construct would be a statement.

tl;dr: we'd like it to be an expression, but without do expressions in the language, it's exceedingly hard to come up with the semantics of "what the RHS should evaluate to".

I personally see no use in pattern matching as statements. All of the compelling use cases are using the match statement as an expression.

switch is a statement, and "replacing every use case of switch" is imo compelling enough alone to warrant pattern matching.

That said, I very much hope we can find a way to allow it to be an expression.

I too have a strong preference for it to be an expression.

The way I code nowadays is very expression-oriented, so an expression-based matching construct would be much more useful to me than a statement-based one.

That said, I'd accept a statement-based implementation if that was the only pathway to getting it in the language. Even just a more powerful, less error-prone replacement for switch is better than nothing IMO.

We can limit it as expression and wait for do expression to save us

painful though it may be, I favor the approach @Jack-Works describes.

https://github.com/tc39/proposal-do-expressions

This proposal is in stage 1 of the TC39 process.

https://github.com/tc39/proposals/blob/master/stage-1-proposals.md

Last Presented: July 2018

https://github.com/tc39/notes/blob/master/meetings/2018-07/july-24.md#update-on-do-expressions

Conclusion/Resolution
?

You guys are bumming me out.

@lyleunderwood do-expressions didn't advance at the last meeting because I need to go write spec text for it, but I'm actively working on it. I think it's pretty likely to advance faster or at the same pace as this proposal.

That's great @bakkot I hope you're right!

@lyleunderwood the last presented is May 2020, the data in the page you refer to is not up to date 馃槤

I wanted to point out that there's a PEP that is very similar to this proposal, and is dealing with a lot of the same trade-offs, including:

  • expression-vs.-statement
  • 'case' vs. special syntax
  • matching against variables
  • etc. etc.

https://www.python.org/dev/peps/pep-0622/

Thanks @mkubilayk for finding this

FYI, the pattern matching proposal for Python is now accepted.

Duplicate of #161.

The proposal has been updated in #174. The match construct is now an expression, using do expression semantics.

Was this page helpful?
0 / 5 - 0 ratings