It's not terrible that I have to use SAM syntax, a...
# announcements
t
It's not terrible that I have to use SAM syntax, and most of the time I can take a Kotlin function instead; this is just me being curious about why the compiler is preventing the trailing lambda call in one place vs. another.
k
https://kotlinlang.org/docs/reference/java-interop.html#sam-conversions
Also note that this feature works only for Java interop; since Kotlin has proper function types, automatic conversion of functions into implementations of Kotlin interfaces is unnecessary and therefore unsupported.
t
I don't think this is the same thing. I'm not implementing a Kotlin interface.
Function
is from the Java stdlib.
(I've also looked at KT-7770, which again is only about Kotlin interfaces with a single abstract method.)
It makes me wonder if there's special-casing for the Streams API + Function, or something like that.
And that announcement you linked is also about defining your own interfaces, which is not what's going on in that code sample.
(although I do think that's good news)
k
I thought you were accidentally using the Kotlin
Function
interface, my bad.
It's not the variance either,
f: Function<in Int, out Int>
still doesn't work.
t
It's weird, right?
I've had a devil of a time asking people about this because everyone thinks I'm talking about KT-7770. 😆
k
Yeah I apologize for annoying you even more :).
It looks like
Comparator.thenComparing
works too, but that could just be special cased as well.
t
If I make a library that exposes something like
takesSAM
, it doesn't work. But that's still a Kotlin-compiled library, so maybe there's some trace of that in the compiled code...
I'll have to find a java library that takes Function and see what the compiler says about calling that.
Ah... Google Guava has some, and I can't use trailing lambda with those! So I think the Java stdlib is special-cased. Example:
Copy code
ImmutableTable.toImmutableTable(
        Function<Int, Int> { it * 2 },
        Function<Int, Int> { it * 2 },
        Function<Int, Int> { it * 2 }
    )
k
The only mention I could find in the compiler is this, but I'm not sure if it's related at all: https://github.com/JetBrains/kotlin/blob/master/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/FakePureImplementationsProvider.kt#L37
I'll try setting up the compiler for debugging tomorrow, should be interesting.
t
Update: I've been told this is actually https://youtrack.jetbrains.com/issue/KT-11129