Mix elements of the two styles, OO and FP

Hello,

I would like examples of mixing OO and FP paradigms in Kotlin. I found no research sources on the subject with simple examples.

“Is Kotlin an object-oriented language or a functional one?
Kotlin has both object-oriented and functional constructs. You can use it in both OO and FP styles, or mix elements of the two.”
Reference: https://kotlinlang.org/docs/reference/faq.html

I’m not sure what your question is exactly. As the docs state: “Kotlin hash both object-oriented and functional constructs”. The two obvious examples are inheritance and higher order functions. Inheritance is an OO feature and higher order functions (the ability to treat functions like any other variable) is the definition of FP.
I’m not sure what kind of example you are looking for. It’s not like combining OO and FP gives you a third system of programming. This part of the docs just mean that you can use paradigms of both styles.

2 Likes

A minimal example is probably really silly like an object whose constructor takes a function:
class Foo(val f:() -> Unit)

Per se, that’s not particulatly exciting.

3 Likes

A good article on FP and OO difference is here (come from ceylon language blog).
In short, OO & FP are similar; but big difference is between FP vs pure FP (only pure function), which is same as OO vs “immutable only” OO. In this sense, Kotlin is FP/OO language (like majority) and haskell is (pure FP / immutable OO) as all objects are immutable.