rob42
06/14/2024, 11:44 AMtype MyInterface interface {
DoSomething() int
}
type MyObject struct {
MyInterface
}
bar := SomeImplementationOfMyInterface()
foo := MyObject { MyInterface: bar }
foo.DoSomething() // -> calls bar.DoSomething()
The closest I can get in Kotlin seems to be manually proxying methods to the wrapped implementation:
class MyObject(val underlying: MyInterface): MyInterface {
override fun DoSomething() = underlying.DoSomething()
}
Which is tedious and breaks if MyInterface changes.
I want to end up with an object that delegates everything to the underlying interface, but adds additional methods.Joffrey
06/14/2024, 11:46 AMclass MyObject(val underlying: MyInterface): MyInterface by underlying {
// you may add or override methods here
}
With the MyInterface by underlying declaration, MyObject automatically implements all methods from MyInterace by delegating their implementation to underlying. You can override some of those if you want to do something different than just delegating. You can also of course add anything else as well in the body.rob42
06/14/2024, 11:49 AMrob42
06/14/2024, 11:50 AMJoffrey
06/14/2024, 11:50 AMLarry Garfield
06/14/2024, 1:39 PMJoffrey
06/14/2024, 1:40 PMthis it will be the underlying object.Larry Garfield
06/14/2024, 1:43 PM