Typealiases and Java integration


Considering the following example:

interface A {
    fun foo(n: Int): String

fun applyPrefix(prefix: String): A = object : A {
    override fun foo(n: Int) = prefix + n

Currently it’s not possible define applyPrefix in a more compact way (as reported in KT-7770):

fun applyPrefix(prefix: String): A = { n -> prefix + n } // syntax error

However now you can use the type aliases, so:

typealias A = (Int) -> String
fun applyPrefix(prefix: String): A = { n -> prefix + n }

This is great, but there is a little problem: it’s not the same thing from a Java perspective.
If you are using this library in a Java project, you see that applyPrefix returns a Function1<Integer, String>, so you cannot benefit from functional interfaces (for example, to search all usages of A functors).
Obviously the java project could define an interface like A converting Function1 properly, but this could be frustracting in a wide project.

In your opinion how this “problem” could be addressed?


I’m not sure what solution you expect to see, other than supporting SAM conversion for Kotlin classes (which may be implemented in a future version of Kotlin). Typealiases do not create new types, therefore, they cannot affect the Java-visible API of a Kotlin class in any way.


As Java developer I would expect an automatic conversion. I’m considering to adopt Kotlin, and sincerely this is the only thing I’ve found that Java seems to do better.