Proposal-pattern-matching: Support multiple parameters to pattern matching

Created on 8 Jul 2017  路  6Comments  路  Source: tc39/proposal-pattern-matching

While JavaScript doesn't have a formal concept of a tuple, matching multiple values in one statement rather than using nested match statements would be nice. A proposed syntax using commas draws from Swift:

match (a, b) {
  { x }, { t }: // handle case
  { y }, { z }: // handle case
  else: throw new Error("unmatched")
}

Without the multiple match syntax, this code expands to the following:

match (a) {
  { x }: match (b) {
    { t }: // handle case
    else: throw new Error("unmatched")
  }
  { y }: match (b) {
    { z }: // handle case
    else: throw new Error("unmatched")
  }
  else: throw new Error("unmatched")
}
design idea

Most helpful comment

@aem Seems you are proposing a solution for a problem which doesn't exists. You read multiple arguments in a function call function(a, b, c, d) as Array (eg.: this.arguments).

There is no sense to try match a data structure (tuples) which doesn't exists on JavaScript. After all it will be a quickfix behind the scenes to translate it to Array, If you want to achieve a data structure match in a real world scenario is much more sane calling matches([a, b, c, d]) and checks:

[{a: 1, b: 2, ...xs}]: {},
[{a: 'jordan', b: 'marie', ...xs}]: {},

rather than matches(a, b, c, d) , is even easier to read the matches

All 6 comments

implicit fall through and && and || has been discussed here https://github.com/tc39/proposal-pattern-matching/issues/7

EDIT: trotyl good catch. I am leaving this here although you are right they are different.

@limeblack This is not talking about fall through, but matching multiple parameters at the same time.

But this is not relevant to || at all, #7 is talking about matching one parameter with several patterns, but this is for marching several parameters with their own patterns.

Yeah, multiple simultaneous parameters to the match. I updated the title to be a bit more clear

@aem Seems you are proposing a solution for a problem which doesn't exists. You read multiple arguments in a function call function(a, b, c, d) as Array (eg.: this.arguments).

There is no sense to try match a data structure (tuples) which doesn't exists on JavaScript. After all it will be a quickfix behind the scenes to translate it to Array, If you want to achieve a data structure match in a real world scenario is much more sane calling matches([a, b, c, d]) and checks:

[{a: 1, b: 2, ...xs}]: {},
[{a: 'jordan', b: 'marie', ...xs}]: {},

rather than matches(a, b, c, d) , is even easier to read the matches

Hey y'all! #65 has gotten merged, and a lot of issues have become irrelevant or significantly changed in context. Because of the magnitude of changes and subtle differences in things that seem similar, we've decided to just nuke all existing issues so we can start fresh. Thank you so much for the contributions and discussions and feel free to create new issues if something seems to still be relevant, and link to the original, related issue so we can have a paper trail (but have the benefit of that clean slate anyway).

As far as multiple patterns go: we decided to move this feature into the "Beyond This Spec" section. It -would- be very nice, and my earlier draft of the proposal included this (both in AND and OR forms), but after discussions and a bit of bikeshedding, it became clear that this would be a much bigger discussion, and I'd rather not block the initial proposal on this, since I consider it non-essential (based on how this sort of feature is not nearly universal in pattern matching implementations).

Was this page helpful?
0 / 5 - 0 ratings