https://kotlinlang.org logo
#announcements
Title
# announcements
t

Tim McCormack

12/08/2019, 2:28 PM
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

karelpeeters

12/08/2019, 2:48 PM
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

Tim McCormack

12/08/2019, 2:49 PM
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

karelpeeters

12/08/2019, 2:52 PM
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

Tim McCormack

12/08/2019, 2:53 PM
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

karelpeeters

12/08/2019, 2:56 PM
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

Tim McCormack

12/08/2019, 3:15 PM
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

karelpeeters

12/08/2019, 6:29 PM
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

Tim McCormack

12/28/2019, 5:45 PM
Update: I've been told this is actually https://youtrack.jetbrains.com/issue/KT-11129
3 Views