Could someone please explain what this could be used for ?
As another commenter said, it's like conditionals on steroids.
21–30 of 254 posts
Could someone please explain what this could be used for ?
As another commenter said, it's like conditionals on steroids.
Could someone please explain what this could be used for ?
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.
I see what you did there...
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"...
What an ugly syntax. They should follow switch/case syntax for consistency.
Could someone please explain what this could be used for ?
So this:
function getStatus(status) {
if (status === 200){
return "Success";
} else if (status === 401){
return "Fail!";
}
}
var myString = getStatus(response.status);
Now becomes this: var myString = match (status) {
{200} => "Success",
{401} => "Fail!"
}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?
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'
}What an ugly syntax. They should follow switch/case syntax for consistency.
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?