Pattern matching?

# Henrik Sommerland (7 years ago)

Why is there no support for pattern matching in JavaScript? I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript. If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Peace!

# Sebastian Malton (7 years ago)

An HTML attachment was scrubbed... URL: esdiscuss/attachments/20170630/d0b38315/attachment

# Henrik Sommerland (7 years ago)

Yes :)

This is roughly what I had in mind:

match obj with {a, b : 5} -> return a + b {a, c : {d, e : true}} -> return a + d 'rof' -> return 'lol'

Where the first clause matches if obj has the property a and the property b that has the value 5. The second clause works the same but recursively. And the third simply checks if obj is equal to 'rof'.

Den 30 juni 2017 16:46 skrev "Sebastian Malton" <sebastian at malton.name>:

Are you thinking kind of like Rust's match statement? I think that something like that would be quite a good addition.

Sebastian

From: henrik.sommerland at gmail.com Sent: June 30, 2017 9:55 AM To: es-discuss at mozilla.org Subject: Pattern matching?

Why is there no support for pattern matching in JavaScript? I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript. If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Peace!

# Sebastian Malton (7 years ago)

An HTML attachment was scrubbed... URL: esdiscuss/attachments/20170630/436f1f3e/attachment-0001

# Henrik Sommerland (7 years ago)

I would pick === without a doubt. One may consider providing some form of optional syntax to do this like {b ~: 5} but I think that the safe way should be the default way. If the decision is made to support both strict comparisons and the silly ones (I'm a bit biased :p ) there could beseveral ways to fix this.

Den 30 juni 2017 17:26 skrev "Sebastian Malton" <sebastian at malton.name>:

# kai zhu (7 years ago)

-1. will pattern matching code written by the vast number of beginner javascript programmers be readable/maintainable? i personally think not. yet another duplicative feature (that can probably be done more maintainably by switch statements or the all-powerful String.prototype.replace) to further muddle the confusing state of "best coding-practices" for javascript.

-kai

On Jun 30, 2017 10:46 PM, "Sebastian Malton" <sebastian at malton.name> wrote:

Are you thinking kind of like Rust's match statement? I think that

something like that would be quite a good addition.

Sebastian

From: henrik.sommerland at gmail.com Sent: June 30, 2017 9:55 AM To: es-discuss at mozilla.org Subject: Pattern matching?

Why is there no support for pattern matching in JavaScript? I would guess that it has been proposed before, and if so why hasn't it

been included?

I have been thinking about it and I think it would integrate nicely into

JavaScript.

If it hasn't been proposed already I would propose it, and I would gladly

look into it more and try to examine what kind of semantics would be suitable.

# Andreas Rossberg (7 years ago)

On 30 June 2017 at 15:55, Henrik Sommerland <henrik.sommerland at gmail.com>

wrote:

Why is there no support for pattern matching in JavaScript? I would guess that it has been proposed before, and if so why hasn't it been included?

I have been thinking about it and I think it would integrate nicely into JavaScript. If it hasn't been proposed already I would propose it, and I would gladly look into it more and try to examine what kind of semantics would be suitable.

Long ago there was a proposal:

web.archive.org/web/20111206195512/http://wiki.ecmascript.org/doku.php?id=strawman:pattern_matching

# Jordan Harband (7 years ago)

If it only does ===, then it's not much better than switch.

It would be interesting to see a pattern matching proposal that was extensible/customizable, perhaps via a well-known Symbol method.

# Alex Deas (7 years ago)

I like pattern matching but don't think it would work well for ES, although I'd love to see a proposal that proves me wrong. My main concerns would be:

  • What are you matching on? Constructor, instanceof, duck typing, prototype etc
  • What happens when one type can be coerced to another.
  • What happens in the case that the matching identifier doesn't exist? - such as with Object.create(null, {})

Alex

# Isiah Meadows (7 years ago)

For those of you still wanting to follow the current state of things, check out the proposal's repo 1, and the issue tracking my alternate proposal 2.


Isiah Meadows me at isiahmeadows.com

Looking for web consulting? Or a new website? Send me an email and we can get started. www.isiahmeadows.com