4 ms·
It suits cases (e.g. modelling business logic) where you have that kind of edge case. Until recently non-OO languages had very little support for delegation, an
by lmm 4y ago
It suits cases (e.g. modelling business logic) where you have that kind of edge case. Until recently non-OO languages had very little support for delegation, and so while inheritance bundles delegation with some other things it really shouldn't be bundled with, it was often the least-bad option. In particular this made it a natural fit for GUI, where you very often want "this widget is almost like this one, except...", and so it was able to ride the coattails of that.
Add on the fact that people somehow seem to think Alan Kay has some insights (even if they can never tell you what they are), and Bob's your uncle.
- substation13 4y ago> non-OO languages had very little support for delegation, What do you mean by "delegation" here?
- lmm 4y agoI mean something like "this value contains this kind of value and implements the same interface as it by forwarding methods to the corresponding methods on that". E.g. https://kotlinlang.org/docs/delegation.html https://kotlinlang.org/docs/delegation.html . (i.e. imagine doing something similar to inheritance but having the "parent" object be explicitly a field of your child object rather than just mushed together with it).
- substation13 4y agoAh I see. What you say is not quite true though, ad-hoc polymorphism (traits) provides the same functionality for non-OOP languages. > Until recently non-OO languages had very little support for delegation
- lmm 4y ago> ad-hoc polymorphism (traits) provides the same functionality for non-OOP languages. No they don't. Show me how you'd do that in e.g. Haskell. And note that Rust has `delegate` as a macro even though it has a trait system.
- JoelMcCracken 4y agoThis is what Haskell uses type classes for. See how Monoid typeclass delegates to semigroup with default implementation of mappend
- lmm 4y agoTypeclass instances themselves can do delegation because they have inheritance (or something very similar to it). The relationship between Monoid and Semigroup is very much like OO subclassing. I'm talking about doing delegation for regular values, not typeclass instances.
- JoelMcCracken 4y agoOh, well then you are talking perhaps using a newtype to wrap another type and perhaps use deriving to get behaviors from underlying type automatically. Or with a record you just explicitly implement typeclass methods in reference to the contained data. If you mean doing it without doing a typeclass, well the functions need to have different names. If that’s exactly what you want. In order to have the same function name you need a typeclass.
- lmm 4y ago> Oh, well then you are talking perhaps using a newtype to wrap another type and perhaps use deriving to get behaviors from underlying type automatically. Deriving sort of does what delegation does, but in a very limited way, and even when it does work you can't use it for user-defined typeclasses, so it's not a solution to the problem except in a handful of special cases. > Or with a record you just explicitly implement typeclass methods in reference to the contained data. The whole point of a delegation feature is that you don't have to do this explicitly for each method.