if(val==1) var res = 1;
if(val==2) var res = 2;JavaScript Pattern Matching Proposal
41–50 of 254 posts
Re: JavaScript Pattern Matching Proposal
#42What an ugly syntax. They should follow switch/case syntax for consistency.
Pattern matching doesn't work the same way as switch/case though. Using that syntax would be misleading and confusing.
Re: JavaScript Pattern Matching Proposal
#43Earlier quoted context omitted.
Is not `switch` enough for this?
It's more concise than switch, as well as more powerful (can destructure objects). You can't set things equal to the result of a switch. I find the pattern very convenient. let day; switch (date) { case 1: day = "mon"; break; case 2: day = "tues"; break; default: day = "wed"; break; vs let day = match(date) { 1 => 'mon', 2 => 'tues' }
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';Re: JavaScript Pattern Matching Proposal
#44Earlier quoted context omitted.
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?
Re: JavaScript Pattern Matching Proposal
#45What an ugly syntax. They should follow switch/case syntax for consistency.
Let's see an example of your proposed alternative syntax.
Re: JavaScript Pattern Matching Proposal
#46Additionally, this would be the first "native" way to do deep equality testing of objects. I can see it being abused to do simple one-off checks that could otherwise have been done more simply in an imperative way.
Do we really need to save the five lines of code at the cost of new syntax? This doesn't seem to prevent any significant amount of toil, since it can be pretty easily unrolled into existing JS syntax.
Re: JavaScript Pattern Matching Proposal
#47What an ugly syntax. They should follow switch/case syntax for consistency.
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 }
Re: JavaScript Pattern Matching Proposal
#48Out of the major, popular languages today, Javascript is easily the worst.
Re: JavaScript Pattern Matching Proposal
#49What an ugly syntax. They should follow switch/case syntax for consistency.
Pattern matching doesn't work the same way as switch/case though. Using that syntax would be misleading and confusing.
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 those two things are functionally different.Granted, it doesn't help make switch any easier to learn, but I _do_ think it makes switch more _rewarding_ to learn. Right now, switch is basically just a restrictive, potentially terser version of an if statement. This would make switch actually useful to learn.
It might even allow us to add some more semantics to switch over time (e.g. constructor matching with something like 'case Number').
(EDIT: thanks to armandososa for teaching me a new thing!)
Re: JavaScript Pattern Matching Proposal
#50Javascript 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"...