Pattern Matching but also Entire Result
Closed this issue · 5 comments
It would be nice to be able to specify both a name for the whole value and also the names for the parts of the value in the LHS (or in a formal parameter). Here's sample syntax:
myobj: { p1: { a: 1, b: 2 }, p2 : { c: 3, d: 4 } }
myp1@{x, y}: myobj.p1
# myp1 == { a: 1, b: 2 }, x == 1, y == 2
The @ is the character that Haskell uses for this feature. CoffeeScript should choose the syntax most appropriate for and integrated with the rest of CoffeeScript for this particular feature.
Does this achieve what you're asking for?
{ a: x, b: y }: myp1: myobj.p1
This assigns myobj.p1 to myp1, then deconstructs it into x and y.
That is nice too. It is not exactly what I had in mind, because it does not operate in a single assignment expression and because it would be difficult to work into the formal parameters.
I think that Tesco has a solid answer ... that a series of partial matches is a fine way to accomplish this without adding a new symbol to the current syntax. And because pattern-matching-in-parameters isn't happening for now (the sister-issue for this, #362), it's less necessary. Closing as a wontfix.
{ a: x, b: y }: myp1: myobj.p1 does not seem to compile on http://coffeescript.org/ anymore?