Re:F# 4.0 Preview
by AndrewDucker on Thursday November 13, 2014 @04:45AM
(#8600703)
Yeah, it looks a lot cleaner - but if you make that code available as a library then other people can't add new shapes easily, or deal with shapes that have more than one length (e.g. a rectangle, which has two).
[ Parent
| on HN ]
Well, yes, you can add a rectangle quite easily:
type Shape =
| Circle of float
| Square of float
| Rectangle of float * float
The thought about adding new shapes (mentioned several times in this thread) is just misplaced. If you're releasing library code people should not be extending your built-in types, that's bad form -- consumers of a library should interact with it through interfaces only.The flipside is that its very easy for your library users write a new function that takes a Shape as a parameter. If you model things with a class hierarchy its easy to add a new Shape subclass but you can't add any new methods to the Shape interface.