Earlier quoted context omitted.
No, not at all. E.g. Erlang is built around pattern matching as a core feature even though it's an untyped language.
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…
JavaScript Pattern Matching Proposal
71–80 of 254 posts
Re: JavaScript Pattern Matching Proposal
#72Isn'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?
No, have a look at Erlang! Pattern matching is one of it's most powerful feature.
I agree with the sentiments of some of the other commenters -- it's much less troublesome to match on tuples (Erlang) and predicate clauses (Prolog) than it is to match on JavaScript objects. With tuples/clauses, the constructor syntax mirrors the pattern matching syntax, so it's trivial to read the code at a purely syntactic level to debug pattern matching issues, and this would simplify automatic static analysis as well. As soon as you can add "fields" to an object at arbitrary (non-local) points in the code, local syntactic analysis becomes intractable.
Re: JavaScript Pattern Matching Proposal
#73Out of the major, popular languages today, Javascript is easily the worst.
Re: JavaScript Pattern Matching Proposal
#74Its an interesting concept but, The choice of syntax seems bad. The definition bit looks like a function definition but behaves entirely differently. Why? Why not make it a constructor like everything else? People could look at it and be like "oh a match object" instead of wondering if you overloaded function. The selectors look like json, only assignable. Again make it obvious.I mean we could even just make it json…
Re: JavaScript Pattern Matching Proposal
#75Re: JavaScript Pattern Matching Proposal
#76Interestingly, the match construct is an expression, which I don't think JavaScript has m/any of. Perhaps they could retroactively make if/else expressions, if that doesn't break back-compat.
This is basically what they could translate into without having if/else or nested ternaries.
I'm not sure if it is much better than either but I like it more.
Re: JavaScript Pattern Matching Proposal
#77I 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.
Re: JavaScript Pattern Matching Proposal
#78Javascript is such a mess, this proposal looks really bad, and the guys discussing it are saying things like: "we should make sure that this doesn't look like anything else, so that people who learn the language don't confuse match and switch..." There is also a lot of "we shouldn't break that" messages, and people reply with "oh we already screwed that up here and there, so it's fine"...
as someone who only has a little experience with pattern matching, how does this look bad? The syntax looks good enough to me that I could and would want to use it.
Here is the syntax for different languages:
- Rust https://doc.rust-lang.org/stable/book/second-edition/ch06-02...
- Haskell http://learnyouahaskell.com/syntax-in-functions
- Scala https://docs.scala-lang.org/tour/pattern-matching.html
Re: JavaScript Pattern Matching Proposal
#79Earlier quoted context omitted.
That's not how it works. You don't have to be Steven Spielberg to dislike some Hollywood movie. How is this fallacies called?
There's a totally different barrier of entry for that than suggesting a syntax. That's not even in the same stratosphere.
Re: JavaScript Pattern Matching Proposal
#80Earlier quoted context omitted.
Let's see an example of your proposed alternative syntax.
Here you are: var x = match (response) { case { status:200 }: true; case { status: 404 }: new NotFoundError; case Number: Math.PI; case SomeClass: 1; case /^http/: "http error"; default: -1; };
The leading curly braces in the linked proposal _are_ a bit odd to see, though the repetition of `case` isn't too fun either.