Sam Garfinkel
04/08/2020, 7:53 PMthis
be passed to an interface delegate when initializing the delegate?
data class Foo(
val name: String
): FooExt by FooExtImpl(this)
interface FooExt {
fun nameExt(): String
}
class FooExtImpl(private val outer: Foo): FooExt {
override fun nameExt() = "Hello, ${outer.name}"
}
I basically need this exact functionality, I’m not able to directly add nameExt
to the data class. If delegates could be initialized inside an init { }
block then that would work but I’m unsure of any actual workarounds for this.Zach Klippenstein (he/him) [MOD]
04/08/2020, 7:59 PMthis
doesn’t exist when you’re creating the delegate – kotlin needs the delegate in order to create the class, so it’s a chicken/egg problem. Even if you were able to access an uninitialized version of the delegating class, it would be really hard to reason about initialization order. This issue already exists with superclasses accessing open members, since they might not be initialized when the superclass constructor runs, and Kotlin will give you warnings about this I believe.Sam Garfinkel
04/08/2020, 8:02 PMdata class Foo(val name: String): FooExt {
private val delegate: FooExtImpl(this)
override fun nameExt() = delegate.nameExt()
}
Sam Garfinkel
04/08/2020, 8:04 PMZach Klippenstein (he/him) [MOD]
04/08/2020, 8:34 PMZach Klippenstein (he/him) [MOD]
04/08/2020, 8:43 PMDavid Eriksson
04/08/2020, 8:58 PMZach Klippenstein (he/him) [MOD]
04/08/2020, 9:00 PMSam Garfinkel
04/08/2020, 9:09 PM