Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access a Kotlin typealias from Java?

Suppose I have a Kotlin 1.1 typealias for a Kotlin function type like this

typealias Consumer<T> = (T) -> Unit

I can access this from Java as

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

Function1<? super T, Unit> action = ...

Is it somehow possible to access the Kotlin Function1 interface from Java under its Kotlin typealias name (i.e., Consumer)?

like image 299
Anlon Burke Avatar asked Oct 03 '17 19:10

Anlon Burke


People also ask

Can you call Kotlin code from Java?

One extremely useful feature in IntelliJ IDEA and Android Studio is the ability to see the compiled bytecode of your Kotlin code and then the decompiled Java code. For this, press Ctrl+Shift+A (Cmd+Shift+A on Mac) to invoke the action search, then type “Show Kotlin Bytecode” (or “skb”) and press Enter.

Can Java and Kotlin be used together?

If your question is can you use kotlin files in java files and vice versa then the answer is yes.

Why is Kotlin interoperable with Java?

Whenever two-byte code files run on JVM, due to their equivalent nature they can communicate with each other and that's how interoperability is established in kotlin for Java. And makes Kotlin 100% interoperable with Java.


1 Answers

From the KEEP proposal for type aliases:

NB Java has no concept of "type aliases" and can't see them in class member signatures.

So no, you can't use typealiases from Java, you'll just see the actual types of any parameter or variable that has a typealias'd type in Kotlin.

like image 90
zsmb13 Avatar answered Oct 16 '22 09:10

zsmb13