https://kotlinlang.org logo
#getting-started
Title
# getting-started
m

martmists

12/14/2021, 11:45 PM
How would I go about implementing something like this:
Copy code
fun <R> convertTo(): R {
    return convertImpl()
}

fun convertImpl(): Int {
    // ...
}

fun convertImpl(): String {
    // ...
}
Is there an easy way to do it or will I have to just use a when statement and make R reified and add a branch for each implemented type?
j

Joffrey

12/15/2021, 12:12 AM
If it were the other way around I guess polymorphism would be best, but since in your case the call site is supposed to decide the output type, you might want a
when
on the reified
R
class.
3 Views