https://kotlinlang.org logo
Title
t

Tolriq

11/03/2018, 7:03 PM
Got a quick question about using a Java API from Kotlin. The API in question is the Cast API from Google that is defined in a strange way.
public final class Cast {
public static final Cast.CastApi CastApi;

    private Cast() {
    }

    public interface CastApi {
   }
}
Accessing the static final field from Kotlin seems impossible AS always think I'm referring to the interface and not the field, is there any trick for that?
e

elizarov

11/03/2018, 7:13 PM
Hm… it works for me:
val api: Cast.CastApi = Cast.CastApi
It resolves to type at the left and to static variable at the right.
t

Tolriq

11/03/2018, 7:17 PM
Hum thanks for the workaround 🙂 I was doing like before in Java and directly calling
Cast.CastApi.function()
and that does not work. Using a val works and allows me to remove a Java file that I used as a bridge.
e

elizarov

11/04/2018, 4:40 AM
I now see what is the problem! You don’t have to use a variable. You can also add an import for
Cast.CastApi
and the a short name
CastApi
would resolve to a variable so that you can use its methods with
CastApi.method()
t

Tolriq

11/04/2018, 12:19 PM
Import hack works too 🙂 Should I raise an issue for the default use case, or is somewhat normal?
e

elizarov

11/04/2018, 12:52 PM
It is so by design.
👍 2