https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Jason5lee

01/12/2022, 10:13 AM
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

Joffrey

01/12/2022, 10:17 AM
What is the signature of
getImplByMyClass
?
j

Jason5lee

01/12/2022, 10:18 AM
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

Joffrey

01/12/2022, 10:19 AM
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

Jason5lee

01/12/2022, 10:23 AM
I want
MyClass
 to implement 
Interface
 by delegating to the object created with 
getImplByMyClass.
Or, using the edited version,
ImplByInterface2
.
j

Joffrey

01/12/2022, 10:27 AM
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

Michael de Kaste

01/12/2022, 10:57 AM
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

Joffrey

01/12/2022, 12:46 PM
@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

Michael de Kaste

01/12/2022, 2:22 PM
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
2 Views