3 ms·
I see, although it looks like you're really after a state-machine there. Which I admit aren't the easiest to create in C#, and immutability and null-safety defi
by contravariant 8y ago
I see, although it looks like you're really after a state-machine there. Which I admit aren't the easiest to create in C#, and immutability and null-safety definitely make it easier...
Although personally I'd rather have the different states encapsulated in separate classes than a single type that encapsulates all possible states and enforces them through the constructor.
- victorNicollet 8y agoThis is what F# does (define multiple classes), but each of these classes only takes one line of code.
- sparkie 8y agoAlso, it defines them as nested classes, and marks the outer class as `sealed`, as they're closed types, and it prevents the type being extended with new cases. C# can't do this because the compiler rejects putting `abstract` and `sealed` on the same type.
- contravariant 8y agoYou can kind of solve this by making the constructor of the outer class private and defining all subtypes as nested types. It's not ideal, but it works.