Kotlin reified type function as function extension - Callable from Java?

Chulo :

I'm trying to use a function with reified type as extension function but I don't think that it's possible because after I checked the generated bytecode I have find that the method signature is private, any work around to make it public ?

CommonExtensions.kt

inline fun<reified T: Activity> Context.startActivity() {
    val intent = Intent(this, T:: class.java)
    startActivity(intent)
}

fun View.visible() {
    visibility = View.VISIBLE
}

Kotlin Bytecode :

private final static startActivity(Landroid/content/Context;)V
    @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0
   ...

Client Code :

Kotlin file

override fun showMessageEmptyOfferFeeds() {
        mOfferFeedsWarning.visible() // "visible()" extension func RESOLVED
}

Java file

showProfileDetailsUi(){
   startActivity<DetailActivity>() //"startActivity()" extension func NOT RESOLVED
}
s1m0nw1 :

Yes you can use inline functions with reified types as extension functions. It's made private so that Java code can't access it (btw this is not the case for "normal" inline functions). Such an inline function can be private for Kotlin because inline functions are copied to the place where they are invoked.

An example:

inline fun <reified T : Activity> Activity.startActivity() {
    startActivity(Intent(this, T::class.java))
}

//usage

startActivity<DetailActivity>()

Read more about reified in another SO question, I answered: https://stackoverflow.com/a/45952201/8073652

Once again: You cannot use inline functions with reified typed from Java.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=477024&siteId=1