4 ms·
// now you kind of enforced to // handle all values, otherwise // it will not compile I fail to see how it will not compile if all values are not handl
by platz 8y ago
// now you kind of enforced to
// handle all values, otherwise
// it will not compile
I fail to see how it will not compile if all values are not handled. C# switch is not traditional pattern matching, because you don't get compile time feedback.
Claiming that is does is disingenuous.
- nikeee 8y agoIn TypeScript, this can be achieved using an assertion to the never type. I don't think it exists in C#, though.
- platz 8y agoThey aren't real guarantees in TypeScript, because the type system doesn't guarantee anything about the actual values that are flowing through the system. i.e. you have to code everything in TypeScript and make sure everyone follows the best practices.
- Dylan16807 8y agoMost statically-typed languages don't verify types at runtime either. As long as you enforce types at the boundaries between typed and untyped code, you should be fine. And enforcing invariants at the boundaries of your code is pretty important for all languages.
- platz 8y agothe point is that in FP languages, pattern matching uses closed ADTs, so you always know at compile time if you've covered all the cases.
- Dylan16807 8y agoHuh, you have do something with returning from a switch in typescript if you want it to make sure all cases are covered. So it's close but not quite there.
- tom_ 8y agoMaybe this type of switch works differently, because there's no good default default case.
- dgritsko 8y agoThis page[1] (which was linked elsewhere in the thread) provides the following info: > Since an expression needs to either have a value or throw an exception, a switch expression that reaches the end without a match will throw an exception. The compiler does a great job of warning you when this may be the case, but will not force you to end all switch expressions with a catch-all: you may know better! I haven't used the feature yet myself, so I can't say it definitively - but it sounds like it does provide compile time feedback while still allowing you to compile. [1]: https://blogs.msdn.microsoft.com/dotnet/2019/01/24/do-more-with-patterns-in-c-8-0/ https://blogs.msdn.microsoft.com/dotnet/2019/01/24/do-more-w...
- platz 8y agoit sounds like it does a "best effort" and then gives up, unfortunately, the article doesn't describe exactly to what lengths it goes here.
- Mindless2112 8y agoSpecifically, it will throw SwitchExpressionException[1] [1] https://github.com/dotnet/corefx/issues/33284#issuecomment-448332714 https://github.com/dotnet/corefx/issues/33284#issuecomment-4...