Re:TypeScript Is Surprisingly OK for Compilers
by paddim8 on Friday August 18, 2023 @03:00AM
(#37172199)
I would think C# is easier for this due to having proper (and very convenient) pattern matching.
[ Parent
| on HN ]
Re:TypeScript Is Surprisingly OK for Compilers
by zkldi on Friday August 18, 2023 @04:25AM
(#37172755)
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)),
};
Re:TypeScript Is Surprisingly OK for Compilers
by hardware2win on Friday August 18, 2023 @04:27AM
(#37172764)
>compiler can't prove the above code is exhaustive because no proper sum types
Thats because c#s enum is not "closed"