Maxim
Maxim

Reputation: 9961

Is it possible to add "static" extension function for Java class?

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?

UPDATE 2019-06-12: This question doesn't answer to my question because my question about compatibility of Kotlin extension functions with Java classes.

Upvotes: 15

Views: 4173

Answers (1)

yole
yole

Reputation: 97148

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.

Upvotes: 28

Related Questions