Hello! Does anybody know how can I use inner classes to delegate interface implementation ?
Copy code
interface Action
class Outer(doer: Doer = this.Doer()) : Action by doer { //'this' is not defined in this context
inner class Doer : Action
}
or
Copy code
interface Action
class Outer : Action by Doer() { //Constructor of inner class Doer can be called only with receiver of containing class
inner class Doer : Action
}
t
thanksforallthefish
04/30/2020, 2:32 PM
why does it have to be
inner
?
inner
classes requires an instance of the outer class to be instantiated. eg
Copy code
val outer = Outer()
val inner = outer.Inner()
and ofc you don’t yet have an instance of the outer class when you are instatiating the outer class itself
thanksforallthefish
04/30/2020, 2:33 PM
Copy code
interface Action
class Outer(doer: Doer) : Action by doer {
inner class Doer : Action
}
is legal, the problem you are having is not delegating, but instantiating
💯 1
y
Yevhenii Nadtochii
04/30/2020, 2:38 PM
Yes, I understand I can make constructor private and create instances in companion object to resolve instantiating problem. But don't want to. I rather can't get to optimal class structure.
Yevhenii Nadtochii
04/30/2020, 2:40 PM
I have Outer class that should implement two interfaces and has some basic PRIVATE functions. I want to delegate each interface to separate classes. But the problem is that they have to use private methods of outer class.