2 ms·
> do one thing when field a is null and field b is not And that would have been a good example! contrary to the one in the docs. Pattern matching for checking
by bernawil 7y ago
> do one thing when field a is null and field b is not
And that would have been a good example! contrary to the one in the docs.
Pattern matching for checking fields of things of the same type is a good use. Pattern matching when checking your parameters type? bad.
> The nice thing about it is the compiler can assert that your match is exhaustive
If you pass a new Shape object for which you forgot to implement a new case in the pattern matcher, you just get the default case, which is wrong, but the compiler can't know that.
Now have the function argument ask for something that implements IArea interface and if you don't implement getArea() in your new Shape class, the compiler will know.
- emodendroket 7y agoWell I think it is pretty common to have a combination of these. You're right that if you configure a default matcher the compiler can no longer tell if the match is as exhaustive as you intended, but I think you can often just avoid that altogether.