My SDK exposes a Java interface that has only static methods, e.g.
public interface MyDevice {
public static void setLocation(Location location) {…}
public static Location getLocation() {…}
}
In Java app that uses the SDK, my customers can use these as if it were a singleton, e.g.
Location currentLocation = MyDevice.getLocation();
When this SDK is integrated into a Kotlin app, it would be natural to express the same as a property:
val currentLocation = MyDevice.location
The problem is, this built-in interop works for non-static methods only.
I can create a singleton in Kotlin and have it handle the translation:
object myDevice {
var location: Location
get() = MyDevice.getLocation()
set(location) = MyDevice.setLocation(location)
}
But won't this single Kotlin file in an otherwise Java-only SDK negatively affect the customers who don't use Kotlin? Can the same be expressed in Java?
Or maybe I should simply convert MyDevice.java to Kotlin? What will be negative effects of such step for the customers who are still on Java?
The problem you described is a lack of meta data Kotlin need to treat static methods as Class extension properties of extension functions.
Together with issue KT-11968 it makes it not possible for now, at least.
The best possible option is API conversion to Kotlin with a support of @JvmStaic/@JvmField and @JvmDefault where necessary for backward compatibility.
interface MyDevice {
companion object {
// nullable type or explicit init
var location: Location?
@JvmStatic get
@JvmStatic set
// kotlin
val x = MyDevice.location
MyDevice.location = x
// java
var x = MyDevice.getLocation();
MyDevice.setLocation(x);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With