Hello! Consider: ```interface IFace { fun test()...
# getting-started
p
Hello! Consider:
Copy code
interface IFace {
  fun test()
  fun test2()
  // etc...
}

class A : IFace {
  override test() {}
  override test2() {}
  // etc...
}

class B : IFace {
  private val a = A() // Is it possible to specify that B must delegate all IFace functions to a without passing a in constructor? 
}
s
to your question: not that I know of. additional note: starting interface names with I is not idiomatic in Kotlin (only in C#, i think!?) Just call your interface
Face
. Ideally you can also avoid calling the implementation
FaceImpl
, but use an implementation detail to name it (e.g.
MultiThreadedFace
).
j
IIRC you can use
class B : IFace by A()
Or if you want access to the instance of
A
, you can also pass it in the constructor but with a default value, so that the callers of B's constructor don't have to specify it:
Copy code
class B(private val a: A = A()) : IFace by a
s
nice circumvention of the initial "without passing it in the constructor"-constraint 👍 Hopefully the reason for the initial constraint doesn't clash with this approach.
p
The reason that I can't pass it in constructor is that in real project this property is not field, it has some computation logic on get. And i want to avoid hand write override all functions of interface to delegate.
j
You should probably have opened with this, then :)
You mean you want every method call to compute a new instance of
A
from the property and then call the method on that
A
p
I want to delegate every method of IFace to a::get. It is not always true that get will return new A, but every call to get can return different instances of A.