Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Static Method in Java as Extension Method in Kotlin

I have a static method declared in Java:

class X {
    public static void foo(Y y) { … }
}

I would love to use this method as extension method for instances of type Y in Kotlin:

import X.foo
…
y.foo()

Is that possible? I have control over all source code in question, e.g. to add annotations.

like image 317
Bombe Avatar asked Sep 09 '17 15:09

Bombe


1 Answers

I don't know of a way to automatically refer to these, but writing your own extension that just wraps the existing method should be possible...

fun Y.foo() = X.foo(this)
like image 198
Todd Avatar answered Sep 28 '22 02:09

Todd