6

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
  • 76,759
  • 17
  • 167
  • 196
TooCool
  • 10,598
  • 15
  • 60
  • 85

2 Answers2

6

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.

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
  • Yes it's only a class level .. but I can't use it out side the file in which it's defined .. my question is how to use it as normal extension functions ? – TooCool Sep 11 '17 at 18:45
  • Try to define it top level. That's a class like Extensions.kt which could contain your function only, without a class around it – s1m0nw1 Sep 11 '17 at 18:46
  • Can you share your code? Also where you use it with the compile error? – s1m0nw1 Sep 11 '17 at 18:49
  • Added more details as you asked – TooCool Sep 11 '17 at 19:02
  • You're trying to call it from Java? This does not work, as I wrote. Make it not reified, normal inline functions are ok for Java! – s1m0nw1 Sep 11 '17 at 19:03
2

inline reified functions 'disppear' after compilation, because reified types don't exist on the JVM. It is a trick by the compiler.

Probably the only reason the private function is there is to cause an error if someone tries to override it during runtime, since the reified function is completely inlined and cannot be overridden.

Kiskae
  • 24,655
  • 2
  • 77
  • 74