proposal-block-params icon indicating copy to clipboard operation
proposal-block-params copied to clipboard

tc39 proposal pattern matching into userland

Open Pyrolistical opened this issue 3 years ago • 4 comments

I really like block params as it would generalize the syntax https://github.com/tc39/proposal-pattern-matching is trying to add.

Which happens to what I incidentally did using with patcom

See example ~~We could probably write a tc39 proposal pattern matching polyfill using a block params polyfill + patcom.~~

Pyrolistical avatar Mar 13 '22 17:03 Pyrolistical

What about block params would work well with pattern matching?

ljharb avatar Mar 13 '22 18:03 ljharb

Sorry, I could have provided an example.

This tc39 proposal pattern matching syntax

when ({ status: 200, body, ...rest }):
  handleData(body, rest)

is the like proposal block params do expression (note the first rest is now a matcher, not destructuring)

when ({ status: 200, body, rest }) do (body, rest) {
  return handleData(body, rest)
}

which is implementable in patcom today as

when ({ status: 200, body, rest }, (body, rest) => {
  return handleData(body, rest)
})

It wouldn't be possible to write a tc39 proposal pattern matching polyfill. I take that back. But you could get very close to the intent.

Pyrolistical avatar Mar 13 '22 19:03 Pyrolistical

Sure, i see how with block params and a when function you could simulate parts of it - but you could do the same with a callback function, no?

ljharb avatar Mar 13 '22 20:03 ljharb

Yes, you can do same with callback, but the motivation of this proposal it to provide better syntax for callback/blocks.

Note this proposal also include a nest block feature, so in pattern match case, it should be possible to write

match (value) do {
  ::when ({ status: 200, body: defined, rest }) do (body, rest) {
    return handleData(body, rest)
  }
}

Actually it's a little bit like Raku pattern match, you could execute any statements in match block.

hax avatar Jul 22 '22 12:07 hax