jeggy
04/10/2019, 1:45 PMabstract class A {
abstract val value: String
abstract fun <T: A> test(): T
}
data class B(
override val value: String,
val value2: String
): A() {
override fun test()
= this.copy(value = "HelloWorld")
}
How can I achieve something like this?diesieben07
04/10/2019, 1:47 PMabstract class A<T : A<T>> {
abstract val value: String
abstract fun test(): T
}
data class B(
override val value: String,
val value2: String
): A<B>() {
override fun test()
= this.copy(value = "HelloWorld")
}
Shawn
04/10/2019, 1:48 PMjeggy
04/10/2019, 1:51 PMfunTakesB(b.test())
, but this fails as b.test() returns A<*>
instead of B
Ruckus
04/10/2019, 1:52 PMb
?jeggy
04/10/2019, 1:52 PMB
diesieben07
04/10/2019, 1:54 PMabstract class A<T : A<T>> {
abstract val value: String
abstract fun test(): T
}
data class B(
override val value: String,
val value2: String
): A<B>() {
override fun test()
= this.copy(value = "HelloWorld")
}
fun funTakesB(b: B) {
}
fun foo() {
val b = B("foo", "bar")
funTakesB(b.test())
}
Ruckus
04/10/2019, 1:54 PMB
it will work fine. Are you sure it's not inferred as an instance of A
?jeggy
04/10/2019, 1:55 PMas T
there, which works fine. Was just wondering if I somehow can get rid of itstreetsofboston
04/10/2019, 2:04 PMabstract class Service<T: A<T>> {
abstract fun extra(any: T)
fun someWork(any: T) {
extra(any.test())
}
}
jeggy
04/10/2019, 2:05 PM