https://kotlinlang.org logo
#announcements
Title
# announcements
j

juliocbcotta

06/27/2019, 1:55 PM
Hi, Would be possible to have a version of this method using generics? So the interface could be
T
Copy code
fun getListener(): MyListenerInterface {
return when {
            parentFragment is MyListenerInterface -> parentFragment as MyListenerInterface
            targetFragment is MyListenerInterface -> targetFragment as MyListenerInterface
            else -> context as MyListenerInterface
        }
}
k

kralli

06/27/2019, 1:58 PM
Yes, with
reified
.
Copy code
interface MyListenerInterface

fun main() {
    val listener = getListener<MyListenerInterface>()
}

inline fun <reified T> getListener(): T {
    return when {
        parentFragment is T -> parentFragment as T
        targetFragment is T -> targetFragment as T
        else -> context as T
    }
}
j

juliocbcotta

06/27/2019, 2:01 PM
ohh, thanks! I messed it up! I over complicated my code in my attempt to do it.
2 Views