4 ms·
It would be nice if languages provided mechanisms to support composition without the bureaucracy. Why should we write a ton of boilerplate just to forward a me
by kstenerud 8y ago
It would be nice if languages provided mechanisms to support composition without the bureaucracy.
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.
- perlgeek 8y agoPerl 6 does, with a "handles" trait: class MyType { has SomeOtherType $.containedObject handles ('MethodA', MethodB => 'SomeOtherName', 'MethodC'); } Very useful when you need it. https://docs.perl6.org/language/typesystem#trait_handles https://docs.perl6.org/language/typesystem#trait_handles
- fanf2 8y agoSimilar in perl5 with Moose or Moo package MyClass; use Moo; has member => ( is => ‘rw’, handles => [qw[ methodA methodB ]] );
- gridlockd 8y agoSome 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.
- zmmmmm 8y agoThat's exactly what Groovy provides with the @Delegate annotation [1] [1] http://docs.groovy-lang.org/2.4.9/html/gapi/groovy/lang/Delegate.html http://docs.groovy-lang.org/2.4.9/html/gapi/groovy/lang/Dele...
- unicornmama 8y agoGolang supports exactly this use case.
- cinnamonheart 8y agoDotty (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
- marcosdumay 8y agoThey do. It's called inheritance.
- chucksmash 8y agoKotlin 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 https://kotlinlang.org/docs/reference/delegation.html [1]: https://github.com/rust-lang/rfcs/pull/2393 https://github.com/rust-lang/rfcs/pull/2393 [2]: https://downloads.haskell.org/~ghc/5.04/docs/html/users_guide/newtype-deriving.html https://downloads.haskell.org/~ghc/5.04/docs/html/users_guid...