4 ms·
Why the heck is case Not(DistanceMetric)(): a syntax error?
by jenrzzz 4y ago
Why the heck is
case Not(DistanceMetric)():
a syntax error?
- momothereal 4y agoBecause the right arm of the 'case' keyword is not actually a statement being executed, but its own syntax element to represent a pattern. It is not expecting two sets of brackets there.
- deleted 4y ago[deleted]
- chrismorgan 4y agoWhat follows the keyword “case” there is a pattern, not an expression. It’s not an imperative construct of code to be executed, but a declarative construct of an expected shape of an object, which may include name bindings. Consider things like “case DistanceMetric(distance=d):” earlier in the article: this checks “is the value an instance of DistanceMetric, and if so, take its distance attribute and bind it to the name d”. So in this case, what would it mean? If the value is an instance of Not, take and bind it to the DistanceMetric name (as is typical for a single positional subpattern), and… uh oh, more parentheses, what to do? There’s no obvious sensible meaning for it, so it’s a syntax error.
- psd1 4y agoThanks, that's helped me with pattern matching in C#