Proposal-pattern-matching: Do we support regex matching nested in structures?

Created on 16 Jun 2021  Â·  3Comments  Â·  Source: tc39/proposal-pattern-matching

The readme example has

match (arithmeticStr) {
  when (/(?<left>\d+) \+ (?<right>\d+)/) as { groups: { left, right } } { process(left, right); }
  else { ... }
}

where the as { groups: { left, right } } is after when ( ... ). However, I think it should have been

match (arithmeticStr) {
  when (/(?<left>\d+) \+ (?<right>\d+)/ as { groups: { left, right } } )  { process(left, right); }
  else { ... }
}

since it can be extended for nested cases:

match (res) {
  when ({
    input: /(?<left>\d+) \+ (?<right>\d+)/) as { groups: { left, right } },
    direction: 'N' | 'S' | 'W' | 'E',
  }) { process(left, right, direction); }
  else { ... }
}
question

Most helpful comment

@mpcsh @ljharb Thanks! I confused as with with in previous examples. https://github.com/tc39/proposal-pattern-matching/issues/188#issuecomment-837783929 works for me.

All 3 comments

Your first two examples seem equivalent to me, so I'm not sure how "should have" figures in. Can you elaborate?

(your third example should certainly work as-is)

I do want to note that there is some debate on the semantics of as and with - see #188 for more context. Do some of the code samples in this comment clear things up for you?

@mpcsh @ljharb Thanks! I confused as with with in previous examples. https://github.com/tc39/proposal-pattern-matching/issues/188#issuecomment-837783929 works for me.

Was this page helpful?
0 / 5 - 0 ratings