tc39 / proposal-pattern-matching

Pattern matching syntax for ECMAScript
https://tc39.es/proposal-pattern-matching/
MIT License
5.46k stars 89 forks source link

skinny arrow alternative to fat arrow #94

Closed elijahdorman closed 6 years ago

elijahdorman commented 6 years ago

Fat arrow overloading makes distinguishing between functions and patterns a little ambiguous. Skinny arrow is not the greatest answer, but still provides more visual indication.


//using fat arrow
var foo = match (blah) {
  {abc} => a => b => a * b * abc
}

//using skinny arrow
var foo = match (blah) {
  {abc} -> a => b => a * b * abc
}
zkat commented 6 years ago

skinny arrows have precedent, but in all languages I looked that used ->, their functions also used ->.

So, while intuitively the fat arrow in both places seems to create an ambiguity, this has not been a problem for existing languages. Can you provide examples of this being a problem for, say, Erlang?

elijahdorman commented 6 years ago

You can get away with a lot when your syntax is already quite simple. Statements being expressions also helps. Case and function equivalency helps. Consistent function declaration syntax helps too.

JS doesn't have these things. Function syntax takes a few forms (statement, function literal, fat arrow, fat arrow without parens). The JS syntax is more complicated. Statements aren't always expressions.

Most importantly, there isn't a proposal for multi-arity (or type) function pattern matching. Equivalency between cases and functions isn't on the table, so they are fundamentally different.

mgwidmann commented 6 years ago

Elixir uses skinny arrow (->) for case statements. https://elixirschool.com/en/lessons/basics/control-structures/#case

And uses the reverse (<-) for pattern matching in for loops: https://elixir-lang.org/getting-started/comprehensions.html#generators-and-filters And for pattern matches in with statements: https://elixirschool.com/en/lessons/basics/control-structures/#with

dantman commented 6 years ago

@mgwidmann That's because Elixir uses skinny arrow for functions: https://elixirschool.com/en/lessons/basics/functions/#anonymous-functions

zkat commented 6 years ago

@elijahdorman Erlang syntax is not something I'd label as "simple". Ditto Elixir. Ditto most "FP" languages not in the Lisp family (see also the syntax soup that is Haskell).

Heck, I consider JS' syntax more straightforward than most of these.

zkat commented 6 years ago

Closing this because the new proposal uses ~>. I assume this syntax will change eventually, but it resolves this issue for now :)