-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
match
keyword
#48
Comments
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 '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 |
@caub I'd consider function overloading more cumbersome/polluting than the |
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]).
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). |
It would be probably better to avoid adding another language keyword, above all this one (String#match, ..)
I'd have loved to do
allowing a transgression for const in that case of pattern matching
The text was updated successfully, but these errors were encountered: