4 ms·
> The point was to show how when you're thinking in a language that supports algebraic types and pattern-matching, you automatically exclude states that can't e
by alkonaut 11y ago
> The point was to show how when you're thinking in a language that supports algebraic types and pattern-matching, you automatically exclude states that can't exist.
That should be the default line of thought in OO too!
I admit it's easier to do with discriminated unions, but that doesn't change the fact that the C# class is still a common but poorly designed type in C#.
The job of any type system is to make invalid states unrepresentable. F# has powerful tools for that, and make it intuitive and succinct. C# has fewer and less powerful tools, but it's still possible (and usually necessary) to use those tools! The equivalence of F# DU's are C# class hierarchies, and the equivalent of pattern matching is virtual methods.
Inheritance and virtual methods is a really verbose and horrible way of going about it, but it's the way of going about it in C#. You have to use classes for the state here
Since there is a deleted time only useful for the "Deleted" state of the user the user state shouldn't be an enum. It's one of the pillars of decent OO design: a you don't have a field in a type that is only used for some state of the type: that state should then be a subtype. The possible state values for the user is ActiveState and DeletedState { DeletedTime }.
If a person wrote the C# class in that way out of ignorance rather than laziness, I bet they would write the F# type as a normal record type too!