Live data from Hacker News

JavaScript Pattern Matching Proposal

github.com

31–40 of 254 posts

Re: JavaScript Pattern Matching Proposal

#35
Pattern matching would be a great addition to JS. I built a pattern matching library[1] with a very similar syntax back in 2015 (although I will be the first to admit that it's a naive implementation). It makes validating/traversing deeply-nested objects much less verbose. I'm excited to see where this proposal goes.

[1] https://github.com/cshepp/Kasai

Re: JavaScript Pattern Matching Proposal

#36

Earlier quoted context omitted.

Smooshes the evaluation of an object that can be many different shapes into an easy to understand operation. I think the HTTP response is a great example. It can have many status codes which can determine how you want to proceed with that response. If you've worked with handling HTTP responses, you've most likely implemented some version of a 'match' operation before.

Is not `switch` enough for this?

From looking at the proposal, there are substantial differences. The biggest is that switch is an equality test on a single value, while pattern matching as proposed allows matching on multiple properties of an object, as well as conditional clauses.

I need to look at it more closely, as some languages have matching be an expression, rather than a statement or block like the conventional switch.

It doesn't add something that you can't already do- tcomb and other libraries offer functions that mimic it- but it changes the way you can express certain ideas without needing them, in a way that is already familiar from other languages.

EDIT: yes, it does look like an expression, which means it can be used in many more places than a standard switch.

Re: JavaScript Pattern Matching Proposal

#37

Interestingly, 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.

That would be really cool. This match feels a lot like Rust's which is good.

Re: JavaScript Pattern Matching Proposal

#38
post #6

Pattern matching seems to be very trendy/popular right now. It's a big jump from JS, but https://reasonml.github.io has very nice pattern matching that you can use, and it integrates with the JS ecosystem very nicely.

It's been "trendy" since the 70's. It's more that more people have experience with it and realize that pattern matching is like conditional statements on steroids.

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

Re: JavaScript Pattern Matching Proposal

#39
post #30
post #23

Isn'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, not at all. E.g. Erlang is built around pattern matching as a core feature even though it's an untyped language.

However Erlang does include a static analyzer (dialyzer), which every production system should be using.

Re: JavaScript Pattern Matching Proposal

#40

What an ugly syntax. They should follow switch/case syntax for consistency.

Let's see an example of your proposed alternative syntax.

That's not how it works. You don't have to be Steven Spielberg to dislike some Hollywood movie. How is this fallacies called?
Post reply on HN