4 ms·
I see the other replies mention Rust/Haskell's traits and typeclasses; but a better example to use imo is Python's duck typing. If both objects `Foo` and `Bar`
by ywei3410 6y ago
I see the other replies mention Rust/Haskell's traits and typeclasses; but a better example to use imo is Python's duck typing.
If both objects `Foo` and `Bar` have a method called `baz`, calling `object.baz` will dispatch to the objects `baz` function, no inheritance needed.
In that way, there are 3 ideas when you have inheritance. The first is reuse of code. The second is a relationship between 2 classes/types such that you get the Liskov Substitution principle. The third is dynamic dispatch based on object (or type/class in most cases).
Almost all FP languages have the third (type classes, traits, generic interfaces, the list goes on) and the first (default implementations of traits/typeclasses) but none have the second.