samuelgoto / proposal-block-params

A syntactical simplification in JS to enable DSLs
205 stars 8 forks source link

tc39 proposal pattern matching into userland #45

Open Pyrolistical opened 2 years ago

Pyrolistical commented 2 years ago

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.

ljharb commented 2 years ago

What about block params would work well with pattern matching?

Pyrolistical commented 2 years ago

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.

ljharb commented 2 years ago

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?

hax commented 2 years ago

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.