by kstenerud · 7y ago · 0 comments
Why should we write a ton of boilerplate just to forward a message to a contained object? Why can't we just have:
type MyType {
member containedObject SomeOtherType
forwardTo(containedObject) MethodA, MethodB as SomeOtherName, MethodC
method SomeNewMethod() {some implementation}
}
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. 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 bazRust 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...
class MyType {
has SomeOtherType $.containedObject handles ('MethodA', MethodB => 'SomeOtherName', 'MethodC');
}
Very useful when you need it. package MyClass;
use Moo;
has member => (
is => ‘rw’,
handles => [qw[
methodA
methodB
]]
);[1] http://docs.groovy-lang.org/2.4.9/html/gapi/groovy/lang/Dele...
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.