Languages like OCaml, F#, Lisp etc. are advertised as primarily functional languages however they also have object oriented features (either built in eg. OCaml & F#) or through well supported libraries (eg. CLOS).
From a pragmatic perspective, are there actual use cases where having such object oriented features are beneficial ?
The only use case that I have been able to truly appreciate is compatibility with other object oriented languages, especially when they share the same underlying runtime, as is the case with F#.
Write your answer…
Why was OOP created?
Most people will say reusability, but function's already got that covered. Classes describe objects and this is something that is very useful. Imagine this, you are building a game, and each character has a unique punchline or weapon, a class can accurately describe this object. Sure this could also be achieved using functions, but classes solve this problem in a neater and more expressive manner.
OOP isn't about creating re-usable classes, it's about creating Usable classes.
Features of OOP:
OOP provides a lot of features that makes it a robust solution. Below are just a few of those features.
- Encapsulation: A strong form of abstraction or information hiding that enforces access restrictions.
- Inheritance: This allows a class to extend another class. This creates a parent-child hierarchy. Some languages even allow multiple class inheritance.
- Polymorphism: is when calling code can be agnostic as to whether an object belongs to a parent class or one of its descendants.
- Composition: For example, a function might call
number_of_limbs()on an object, which will work whether the object is of class
Yes, there are benefits. OOP always has its benefits. Sure there are languages that are more on the functional side, that doesn't mean that they can not have a touch of OOP. OOP done right is a really beautiful thing.
I'm not sure about the very details of both patterns but there's some clues we may follow.
Functional programming may solve all problem, but unlikely to solve all in easy ways. And object oriented programming is very different and some parts of it may help making things that is hard in functional programming easier, read about the expression problem here: http://eli.thegreenplace.net/2016/the-expression-problem-and-its-solutions/
The Dev Community
Free, friendly and inclusive platform for developers
Or connect with