It would be probably better to avoid adding another language keyword, above all this one (String#match, ..)
I'd have loved to do
function foo({a, b}) { return 'matches a and b' }
function foo({a}) { return 'matches a only' }
const bar = (a, {b}) => '...';
const bar = (a, {c}, e) => '...';
allowing a transgression for const in that case of pattern matching
Does that not clash with argument destructuring?
Does that not clash with argument destructuring?
It does. It was actually difficult for me to figure out what that code block was supposed to do.
As for the keyword issue, I'd think match is probably going to be a contextual keyword similar to async, which is what allows this to still work:
'use strict'
const async = 'foo'
async function bar () {
return async
}
bar().then(value => console.log(value === 'foo'))
// -> true
my code example above is just more or less like erlang/haskell, but right, it wouldn't work well
Using this match keyword is quite polluting in my opinion
@caub I'd consider function overloading more cumbersome/polluting than the match keyword, but thats just MHO.
I like match because it's what other pattern matching languages use. Principle of least surprise.
Coming from the erlang world, function overload is quite handy:
fact(N) when N>0 -> N * fact(N-1);
fact(0) -> 1.
same as
fact(N = 0) -> 1;
fact(N) -> N * fact(N-1).
Also, it'd be aligned with Typescript function overload.
Having said that, I'm not sure of all the implications it could have with JS, though.
Two more examples of use case of pattern-matching in erlang:
greet(male, Name) -> io:format("Hello, Mr. ~s!", [Name]);
greet(female, Name) -> io:format("Hello, Mrs. ~s!", [Name]);
greet(_, Name) -> io:format("Hello, ~s!", [Name]).
case ... of expression:beach(Temperature) ->
case Temperature of
{celsius, N} when N >= 20, N =< 45 ->
'favorable';
{kelvin, N} when N >= 293, N =< 318 ->
'scientifically favorable';
{fahrenheit, N} when N >= 68, N =< 113 ->
'favorable in the US';
_ ->
'avoid beach'
end.
same as
beach(_Temperature = {celsius, N}) when N >= 20, N =< 45 -> 'favorable';
beach(_Temperature = {kelvin, N}) when N >= 293, N =< 318 -> 'scientifically favorable';
beach(_Temperature = {fahrenheit, N}) when N >= 68, N =< 113 -> 'favorable in the US';
beach(_Temperature) -> 'avoid beach'.
% `_Temperature` is a variable which value is ignored by the compiler as it starts with an underscore.
Hey y'all! #65 has gotten merged, and a lot of issues have become irrelevant or significantly changed in context. Because of the magnitude of changes and subtle differences in things that seem similar, we've decided to just nuke all existing issues so we can start fresh. Thank you so much for the contributions and discussions and feel free to create new issues if something seems to still be relevant, and link to the original, related issue so we can have a paper trail (but have the benefit of that clean slate anyway).
Most helpful comment
It does. It was actually difficult for me to figure out what that code block was supposed to do.
As for the keyword issue, I'd think
matchis probably going to be a contextual keyword similar toasync, which is what allows this to still work: