4 ms·
Ah 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 la
by substation13 4y ago
Ah 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.