News for hackers. Stuff that matters.
Re:Inheritance versus Composition by kstenerud on Thursday May 09, 2019 @03:05AM (#19866034)
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.
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)
Re:Inheritance versus Composition by perlgeek on Thursday May 09, 2019 @04:16AM (#19866322)
Perl 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

Re:Inheritance versus Composition by fanf2 on Thursday May 09, 2019 @03:35PM (#19871498)
Similar in perl5 with Moose or Moo

  package MyClass;

  use Moo;

  has member => (
    is => ‘rw’,
    handles => [qw[
      methodA
      methodB
    ]]
  );
Re:Inheritance versus Composition by zmmmmm on Thursday May 09, 2019 @05:56AM (#19866762)
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.
Re:Inheritance versus Composition by gridlockd on Thursday May 09, 2019 @04:22AM (#19866355)
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.