``` abstract class A { abstract val value: Str...
# announcements
j
Copy code
abstract 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?
d
Copy code
abstract 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")
}
👆 1
s
Recursive templates are about the only way you can approximate self types on the JVM unfortunately
j
I tried this way, this results in getting A back. I have some other function that takes B in, so I want to be able to call
funTakesB(b.test())
, but this fails as b.test() returns
A<*>
instead of
B
r
What is
b
?
j
an instance of
B
I'll just cast it, I know it will always be the right one
d
I can't reproduce what you're reporting:
Copy code
abstract 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())
}
This works fine
r
If it's an instance of
B
it will work fine. Are you sure it's not inferred as an instance of
A
?
j
probably, I will write a better example. Wait a min
I don't find any easy way of showing the use case for this, as I'm working in a bigger project. But this is it: https://pl.kotl.in/B1SSwdiF4
I just used
as T
there, which works fine. Was just wondering if I somehow can get rid of it
s
Copy code
abstract class Service<T: A<T>> {
    abstract fun extra(any: T)
    fun someWork(any: T) {
        extra(any.test())
    }
}
j
Ah, sorry. That was obvious xD
Thanks for the help
👍 1