3 ms·
Nobody says you have to use sum types to solve all problems in the design domain - and as kvb points out, F# does have OO functionality too. Your proposed prob
by thoth 11y ago
Nobody says you have to use sum types to solve all problems in the design domain - and as kvb points out, F# does have OO functionality too.
Your proposed problem might be better solved with the Option type and a simple guard to return None if out of range.
>comically easy to avoid using a standard OO approach
This is an ironic statement given that your code, as written, is circumvented by subclassing Range and side-stepping its constructor. I suppose you actually meant "sealed class"... but we're getting off in the weeds again, missing the fundamental point of the post.
- thomasz 11y ago> This is an ironic statement given that your code, as written, is circumvented by subclassing Range and side-stepping its constructor. You are the second one who says this. I must admit that I do not understand how this would be possible. The properties are not virtual, and not writable for anything but the base constructor. To my knowledge, it's impossible avoid the base classes constructor by subclassing. I'm pretty sure you can actually circumvent the assert, but you would have to use the heavy weapons in the reflection API. class Range { public int From { get; } public int To { get; } public Range(int from, int to) { Assert(from <= to); From = from; To = to; } } class LolRange : Range { public LolRange() : base(0,1) { From = 1; To = 0; } } Error CS0200 Property or indexer 'Program.Range.From' cannot be assigned to -- it is read only Error CS0200 Property or indexer 'Program.Range.To' cannot be assigned to -- it is read only