Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I actually think C# (and many OOP languages) gets around this fairly well, by having virtual and extension methods. Many types of functions can be added easily without needing to go back through every inheritor.


* EDIT - realised I responded with an answer not related to the topic; I'll leave this here as a point-of-reference for the OO side of F# *

Extension methods don't extend the type. They extend the functionality attached to the type.

F# has these too [1] (actually more powerful versions):

The thing about using sum-types (the original Shape example) is that they're not supposed to be extended. You're defining the range of the type, which allows for powerful inference of completeness when using them. If you need extensible classes then F# can do those too, it can represent interfaces, abstract classes and concrete classes [2][3][4], and can even generate ad-hoc classes from an interface by using Object Expressions [5].

I personally try to stick to the pure functional approach, but it can be very handy when interoperating with the .NET framework, or other C# based libraries.

[1] http://fsharpforfunandprofit.com/posts/type-extensions/

[2] http://fsharpforfunandprofit.com/posts/classes/

[3] http://fsharpforfunandprofit.com/posts/inheritance/

[4] http://fsharpforfunandprofit.com/posts/interfaces/

[5] http://fsharpforfunandprofit.com/posts/object-expressions/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: