4 ms·
> "In this case it's a sum type called Shape that ranges over Circle and Square" How do I extend Shape to include Triangle without modifying the current code (
by CmonDev 12y ago
> "In this case it's a sum type called Shape that ranges over Circle and Square"
How do I extend Shape to include Triangle without modifying the current code (e.g. in a separate file)?
- noblethrasher 12y agoYou would have to modify the code, and as I mentioned, it can be pretty painful in F# and other ML languages if you already have a bunch of functions that apply to Shape. Subclassing is a good and useful tool that simply works better in C# than F#. But, just as importantly, class hierarchies are often a poor way to model a problem.
- CmonDev 12y agoFair enough, thanks for answers.
- noblethrasher 12y agoYou're welcome. It just occurred to me that you may have been asking about extending Shape in the C# example. In that case, you'd just subclass Shape as normal[1]. Some people argue that sum types shouldn't be extended, but I never really understood that. Basically, C# lets you define unbounded sum types (in the form of subclassing) whereas idiomatic F# more or less demands that you know up front how many variants you're going to need. [1] Although, sometimes I do define my classes to prevent this by marking the constructor(s) of the abstract base class as private, and then writing the derived class definitions within the body of the base class.
- CmonDev 12y agoI have seen opinions that C# is good for architecture and F# is good for implementation of components. I might opt for such an approach. Really wish I had a default non-null immutability in C# though.