Skip to content
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

Closed
caub opened this issue Aug 16, 2017 · 8 comments
Closed

match keyword #48

caub opened this issue Aug 16, 2017 · 8 comments

Comments

@caub
Copy link

caub commented Aug 16, 2017

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

@f5io
Copy link

f5io commented Aug 16, 2017

Does that not clash with argument destructuring?

@haltcase
Copy link

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

@caub
Copy link
Author

caub commented Aug 17, 2017

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

@f5io
Copy link

f5io commented Aug 17, 2017

@caub I'd consider function overloading more cumbersome/polluting than the match keyword, but thats just MHO.

@davedx
Copy link

davedx commented Aug 23, 2017

I like match because it's what other pattern matching languages use. Principle of least surprise.

@kael
Copy link

kael commented Mar 25, 2018

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.

@kael
Copy link

kael commented Mar 25, 2018

Two more examples of use case of pattern-matching in erlang:

  • Function overload (without recursion):
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.

@zkat
Copy link
Collaborator

zkat commented Mar 25, 2018

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).

@zkat zkat closed this as completed Mar 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants