Kien Vu
07/20/2023, 5:27 AMinterface ISome
class SomeImpl: ISome {
fun doOtherThingsNotInISome() {...}
}
class Component(private val some: SomeImpl = SomeImpl())
: ISome by some {
fun main() {
some.doOtherThingsNotInISome()
}
}
Richard Hajdu
07/20/2023, 6:41 AMsome
in Component
then I don't think there's any other way.
Although, if you don't need the reference to the delegate, but you only want to specify a common functionality of the interface to reuse easily, then you can instantiate the delegate at place.
class Component : ISome by SomeImpl() {...}
Kien Vu
07/20/2023, 7:25 AMsome
I'd like to change but don't want to expose that in delegating class.
The use case for this is that I want to gather some capabilities implementation inside a class and reuse it in multiple places,
consider this:
interface HasDialog {
fun onShowDialogClick()
fun onDialogBtnClick()
}
class DialogImpl : HasDialog {
private val dialogCtrl = MutableStateFlow(false)
override fun onShowDialogClick() = show()
override fun onDialogBtnClick() = dismiss()
fun dismiss() { dialogCtrl.value = false }
fun show() { dialogCtrl.value = true }
}
interface Component: HasDialog {
fun actionA()
}
class ComponentImpl(val dialogImpl: DialogImpl= DialogImpl()): Component, HasDialog by dialogImpl {
override fun actionA() {
//...
dialogImpl.dismiss()
}
}
Adam S
07/20/2023, 8:21 AMfun doOtherThingsNotInISome()
to interface ISome
be possible? I’m curious, because maybe there’s another way of solving the problem you’re trying to solveKien Vu
07/20/2023, 9:27 AMComponent
etc), since I'd intend to make Component implements several capabilities, (HasDialog, HasSnackBar etc...)
(sorry I've editted the above comment)Kien Vu
07/20/2023, 9:28 AMAdam S
07/20/2023, 9:31 AMclass ComponentImpl(val dialogImpl: DialogImpl= DialogImpl()): Component, HasDialog by dialogImpl
could be simplified to
class ComponentImpl(val dialogImpl: DialogImpl= DialogImpl()): Component by dialogImpl
Which doesn’t help much - just making an observation :)Adam S
07/20/2023, 9:33 AMIn general I’d like to hide implementation details as much as possible.Are you trying to hide implementations just internally to make the code cleaner & simpler, or are you publishing a library and you want to hide things from library users?
Kien Vu
07/20/2023, 9:37 AM