3

i want to add some extension function to an already defined class‘s companion object, like

fun Toast.Companion.showImageToast(str : String) {
}

and also i like to do the same thing on interfaces, e.g

fun Runnable.Companion.mainHandler() {
}

I've consulted the documentation, only to find syntax for define extension function for companion object in a user defined class, not for class that don't have a companion object

can i have any chance to do this in kotlin ?

Minami
  • 963
  • 6
  • 21

1 Answers1

8

In Kotlin 1.0, if a class does not define a companion object, it is not possible to define extension functions for that companion object. It's also not possible to define extension functions for Java classes and interfaces such as Runnable.

Alexander Udalov
  • 31,429
  • 6
  • 80
  • 66
yole
  • 92,896
  • 20
  • 260
  • 197
  • thx guy, I've got your answer in another post [link](http://stackoverflow.com/questions/36023617/is-it-possible-to-add-static-extension-function-for-java-class) – Minami Mar 16 '16 at 09:36