Re:Inheritance versus Composition
by cinnamonheart on Thursday May 09, 2019 @08:32AM
(#19867659)
Dotty (Scala 3) recently added support for forwarding to a contained object via an 'export' keyword.
class A {
def foo(): String = ...
def bar(): String = ...
def baz(): String = ...
}
class B {
private val a: A = new A()
def foo(): String = ...
// Export a's methods as b's, except for foo, and rename baz as qux
export a.{foo => _, baz => qux, _}
}
val b = new B()
b.foo() // b's implementation
b.bar() // a's
b.qux() // a's baz
Re:Inheritance versus Composition
by chucksmash on Thursday May 09, 2019 @03:51PM
(#19871670)
Kotlin has this[0].
Rust is talking about adding it as well[1].
I guess Haskell does too[2].
[0]: https://kotlinlang.org/docs/reference/delegation.html
[1]: https://github.com/rust-lang/rfcs/pull/2393
[2]: https://downloads.haskell.org/~ghc/5.04/docs/html/users_guid...
Perl 6 does, with a "handles" trait:
class MyType {
has SomeOtherType $.containedObject handles ('MethodA', MethodB => 'SomeOtherName', 'MethodC');
}
Very useful when you need it.Similar in perl5 with Moose or Moo
package MyClass;
use Moo;
has member => (
is => ‘rw’,
handles => [qw[
methodA
methodB
]]
);That's exactly what Groovy provides with the @Delegate annotation [1]
[1] http://docs.groovy-lang.org/2.4.9/html/gapi/groovy/lang/Dele...
Re:Inheritance versus Composition
by marcosdumay on Thursday May 09, 2019 @01:17PM
(#19870150)
They do. It's called inheritance.
Re:Inheritance versus Composition
by unicornmama on Thursday May 09, 2019 @07:11AM
(#19867082)
Golang supports exactly this use case.
Some languages do specify Mixins, but even multiple inheritance is better than the oversold "composition pattern".
The composition pattern is just one of the many ways Java programmers figured out to write more code that does essentially nothing.
That's why Java programmers will be in demand forever, whereas more productive programmers will optimize themselves out of a job.
Why should we write a ton of boilerplate just to forward a message to a contained object? Why can't we just have:
Then the compiler can optimize the path to call the delegated object directly rather than through a secondary call via this object, since it knows what our intent is.