Is it possible to add “static” extension function

2019-04-19 20:35发布

问题:

For example I have Java class (from external library):

class A {} // This is Java class

I want to add extension functions written on Kotlin and call it as:

A.foo() // This is call of extension function `foo` from Kotlin code

As I understand, right now it is impossible to do in Kotlin because it support "static" extension functions for KClass-es with companion object only. Right?

Seems like nothing to prevent to implement such functionality in Kotlin later. Right?

回答1:

You're right. In Kotlin 1.0, you can define extension functions on a companion object of a Kotlin class, and such functions can be called using the A.foo() syntax. Support for defining static extension functions on Java classes is a possible feature for future versions of Kotlin, but it's not on the roadmap of Kotlin 1.1 at this time.



标签: kotlin