I want to delegate the interface implementation to...
# getting-started
j
I want to delegate the interface implementation to an instance constructed using 
this
.
Copy code
class ImplByInterface2(val a: Interface2): Interface1 {...}

class MyClass : Interface1, Interace2 {
    // Delegate Interface1 implementation to this
    private val impl: Interface1 = ImplByInterface2(this)

}
I found it not easy to use
by
. Is there any convenient way? Both language feature and IDE trick are welcomed. Edit: makes it clearer.
j
What is the signature of
getImplByMyClass
?
j
Ok seems like the implementation will be slightly different so maybe I need to do it manually. The answers are still welcomed as they will be helpful when I met this issue next.
@Joffrey
(AnotherInterfaceMyClassImplements) -> Interface
j
Ok thanks. Now what is the goal exactly here, I'm not sure what you are trying to do. Do you want
MyClass
to implement
Interface
by delegating to the object created with
getImplByMyClass
? Or do you want the
impl
field of
MyClass
to delegate
impl.x()
calls to
this
?
j
I want
MyClass
 to implement 
Interface
 by delegating to the object created with 
getImplByMyClass.
Or, using the edited version,
ImplByInterface2
.
j
Then it looks like you have a conceptual circular dependency here 🤔
MyClass
needs to be constructed before you can pass it to
getImplByMyClass
- or in the edited sample, before you pass it to
ImplByInteface2
. But it cannot be constructed if you can't setup the delegation, which requires the result of that call. Chicken-and-egg problem. You'll need to break that cycle somehow, and the way to do it may depend on what your business is (so it makes sense). It's a bit hard to tell which approach would be best with this abstract example. Can't you implement
Interface2
via a separate object, and implement both interfaces by delegation in
MyClass
?
m
I think the namings might be a bit off, I'm not sure I understood you correctly, and that this is the solution that you wanted. But we had a deep-constructor problem as well where a subfield of a class could be constructed by the constructor
Copy code
interface Interface1
interface Interface2 {
    val x: Int
    val y: Int
    val z: Int
}
class Interface2Impl(
    override val x: Int,
    override val y: Int,
    override val z: Int
) : Interface2

class MyClass private constructor(
    interface2Impl: Interface2
) : Interface1, Interface2 by interface2Impl {
    private constructor(
        x: Int,
        y: Int,
        z: Int
    ) : this(Interface2Impl(x, y, z))
}
you can't still construct a delegation in the primary constructor, but you can create stuff in a secondary one. You could even have a
Copy code
private constructor(
        x: Int,
        y: Int,
        z: Int
    ) : this(
            run {
                your complicated code here
            }
        )
j
@Michael de Kaste I don't think this is relevant for the OP's question, because the whole problem is about
Interface2Impl
needing an instance of
Interface1
to implement
Interface2
(using your reversed naming here)
m
I indeed wasn't sure about what OP wanted precisely, or what the use case was, it just reminded me of our standardimpl use case we needed for our project a while back.
👌 1