Leaving aside syntax preferences or other superficial concerns, I find it discouraging that "Motivating Examples" for a proposal include direct references to particular libraries, or particular libraries' examples. I mean, that one (of two) motivations for adding a feature to a language is "Terser, more functional handling of Redux reducers", just feels wrong and casual.
JavaScript Pattern Matching Proposal
81–90 of 254 posts
Re: JavaScript Pattern Matching Proposal
#82Earlier quoted context omitted.
> It's been "trendy" since the 70's. It's been possible since the '70s but I've seen a lot more talking about it in the last 5-10 years.
Yeah, this applies to all that old Lisp features.
[1] https://github.com/norvig/paip-lisp/blob/master/lisp/patmatc...
Re: JavaScript Pattern Matching Proposal
#83Could someone please explain what this could be used for ?
```
switch(foo)
{
case TextBox t:
Console.Writeline(t.Text);
break;
case TextBox when t.Text = "Bob":
Console.WriteLine("Hello Bob");
break;
case Combobox c:
Console.WriteLine($"{c.SelectedItem}");
break;
case null:
Console.WriteLine("Ooops, null!");
break;
case int i when i == 5:
Console.WriteLine("got an int, and it was 5!");
break;
case default:
// handle the default case.
break;
}
```IN languages like F#, pattern matching can compile time check you have covered all bases as well.
e.g, this won't build
````
type VariableResult =
| E of string
| V of string
let result = V "variable"
match result with
| E e -> printf "was error"
```As i haven't told it how to handle the V case for that discriminated union. So you get nice compile time checking.
Ugh, how do you format code?
Re: JavaScript Pattern Matching Proposal
#84Earlier quoted context omitted.
In fairness I think that would look a bit more like this: const getDate = (date) => { switch (date) { case 1: return 'mon' case 2: return 'tues' default: return 'wed' } } let date = getDate(1) Which is, of course, still less terse. What I normally use when I have cases like this is an object-as-a-map. IE: const dates = { 1: 'mon', 2: 'tues' } let day = dates[3] || 'wed';
By the way, I don't think it is a good idea to write a function as a constant. Please compare this function getDate(date) { .. } to this: const getDate = (date) => { ... } The first version is more readable. We instantly see that it is a function and in a second case it looks like a constant at first. Also without the equal and arrow sign it looks simpler.
new Promise(function (resolve, reject) {
methodOne(data, function (error, response) {
if (erorr) {
reject(error);
} else {
resolve(response);
}
})
})
vs new Promise((resolve, reject) => methodOne(data, (error, response) => error ? reject(error) : resolve(response)));Re: JavaScript Pattern Matching Proposal
#85Earlier quoted context omitted.
Pattern matching doesn't work the same way as switch/case though. Using that syntax would be misleading and confusing.
I think there's an argument to be made that switch statements already do something adjacent to pattern matching. At the least, it's close enough that something like: switch(expr) { case 'foo': break; case { foo: bar }: break; } Wouldn't strike me as all that strange. It just shifts the semantics from "are you exactly this" to "do you look like this". For non-object primitives (e.g. string or number), I don't think th…
switch(expr) {
case 'foo': break;
case { foo: bar }: break;
}
https://news.ycombinator.com/formatdocRe: JavaScript Pattern Matching Proposal
#86Earlier quoted context omitted.
Erlang was built from the ground-up with pattern matching in mind, so it works. Instead objects are jammed into the pattern matching system, and where the two disagreed, pattern matching won. (Indeed, Erlang doesn't really even have objects, just some object-like convention.) In a language that started with an object model that has grown a lot of features, trying to jam pattern matching into it after the fact grows a…
I believe Erlang inherited its pattern matching from Prolog term unification since Erlang was prototyped as a DSL using Prolog's op built-in predicate (plus other Prolog parsing DSLs such as definite clause grammars). [1]: http://www.swi-prolog.org/pldoc/man?predicate=op%2f3
Re: JavaScript Pattern Matching Proposal
#87Isn't pattern matching interesting only in a strongly typed environment where the compiler can statically check that you are giving instructions for all possible cases?
While it's obviously safer and super helpful to have a compiler guarantee that there are checks for all cases, having a non-exhaustive pattern match call still be a big improvement over imperative checking of specific fields for control flow. What I've seen doing fairly fast and loose prototyping and product iteration in Elixir over the last 2 years, is that patten matching can help create better boundaries and data…
Exactly. That's one of the key points I make when talking about Erlang: the = sign specifically, and pattern matching more broadly, are like having full-time, production assertions everywhere.
One key to the success of that in Erlang/Elixir, of course, is that you have the infrastructure and language support to manage widespread assertions that can fail.
Re: JavaScript Pattern Matching Proposal
#88Earlier quoted context omitted.
I think match cases should be functions, since you have built those anyway. So instead of match (x) { expr... } you would have something like match(x, [ fn... ]) or match(x) { name: fn, name2: fn2 } where fn is like (objectToMatch) => { expr }
It could look like this: var x = match (response) { case { status:200 }: true; case { status: 404 }: false; case Number: 0; case SomeClass: 1; default: -1; };
var x = (switch(true){
case response === { status: 200 }:
return true;
case response === { status: 404 }:
return false;
case response === Number:
return 0;
case response === SomeClass:
return 1;
default:
return -1;
});