Can extension functions be called in a “static” wa

2019-04-08 14:13发布

问题:

Is it possible to create an extension function and call it as if it were static?

For Example...

fun System.sayByeAndExit() {
    println("Goodbye!")
    System.exit()
}

fun main(args: Array<String>) {
    System.sayByeAndExit() // I'd like to be able to call this
}

I know that the code sample doesn't work...

  • I understand that kotlin's extension functions are resolved statically, as mentioned in the Kotlin Reference (Extension Functions), but this does not mean they can be called as if they were static functions within a class (in a Java sense).

  • I also understand that this code will not work because there is no instance of System to pass into the method that the compiler will generate; therefore it won't compile.

Why would I want this?

Some of you might be wondering why this behaviour is desirable. I can understand why you would think that is isn't, so here are some reasons:

  1. It has all of the benefits that standard extension functions give.
  2. An instance of the class doesn't need to be created just to access the extra functionality.
  3. The functions can be accessed from a system-wide context (provided the class is visible).

To summarise...

Does Kotlin have a way to "hook" a static function onto a class? I'd love to know.

回答1:

You are really asking for "extension functions for a Class reference" or "adding static methods to existing classes" which was covered by another question here: How can one add static methods to Java classes in Kotlin which is covered by a feature request KT-11968

Extension functions cannot be added to anything that does not have an instance. A reference to a Class is not an instance and therefore you cannot extend something like java.lang.System. You can however extend a companion object of an existing class. For example:

class LibraryThing {
    companion object { /* ... */ }
}

Allows you to extend LibraryThing.Companion and therefore calling some new myExtension() method would look like you are extending the Class reference itself, when really you are extending the singleton instance of the companion object:

fun LibraryThing.Companion.myExtension() = "foo"

LibraryThing.Companion.myExtension() // results in "foo"
LibraryThing.myExtension() // results in "foo"

Therefore you might find some Kotlin libraries add empty companion objects just for this case. Others do not, and for those you are "out of luck." Since Java does not have companion objects, you cannot do the same for Java either.

The other commonly requested feature is to take an existing Java static method that accepts an instance of a class as the first parameter, and make it behave as an extension function. This is tracked by issues KT-5261, KT-2844, KT-732, KT-3487 and probably other feature requests.



回答2:

You can define extension function for an object and use it from system-wide context. An object will be created only once.

object MyClz

fun MyClz.exit() = System.exit(0)

fun main(args: Array<String>) {
    MyClz.exit()
}

Or

class MyClz {
    companion object
}

fun MyClz.Companion.exit() = System.exit(0)

fun main(args: Array<String>) {
    MyClz.exit()
}