4 ms·
C# doesn't have anything resembling proper pattern matching. C#s pattern matching is akin to a marginally improved switch or if/else-if chain. C#s pattern match
by zkldi 3y ago
C# doesn't have anything resembling proper pattern matching. C#s pattern matching is akin to a marginally improved switch or if/else-if chain. C#s pattern matches aren't exhaustive, and the compiler doesn't properly check them at all.[0]
There are no proper sum types in C#, so 80% of the point isn't even there.
enum Season
{
Spring,
Summer,
Autumn,
Winter
}
...
int PatternMatch(Season season) =>
season switch {
Season.Spring => 1,
Season.Summer => 2,
Season.Autumn => 3,
Season.Winter => 4,
// compiler can't prove the above code is exhaustive because no proper sum types
// compiler needs nonsensical branch here that diverges
_ => throw new ArgumentException("Invalid enum value for command", nameof(command)),
};
- hardware2win 3y ago>compiler can't prove the above code is exhaustive because no proper sum types Thats because c#s enum is not "closed" https://github.com/dotnet/csharplang/issues/3179 https://github.com/dotnet/csharplang/issues/3179
- deleted 3y ago[deleted]
- paddim8 3y agoThese things are not about pattern matching but about other language features.