Slackbot
08/14/2026, 11:36 AMYoussef Shoaib [MOD]
08/14/2026, 11:56 AMfun map(f: () -> Unit): F = F(f)Youssef Shoaib [MOD]
08/14/2026, 12:02 PMfun main() {
val f: () -> Unit = {}
useF(f)
}
fun useF(f: F) {}
fun map(f: () -> Unit): F { return { f() } }
I think this means lambda literals can always be SAM converted, and function arguments can as well.
In your case, you have a non-literal and it's not a function argument, hence why it doesn't workYoussef Shoaib [MOD]
08/14/2026, 12:04 PMMarat Akhin
08/14/2026, 12:25 PM() -> Unit).
2. For the purposes of type inference, we often treat a fun interface type as its corresponding functional type (e.g., MyContract => () -> Unit ).
3. In some cases, we allow to put a square peg into a round hole, aka to put () -> Unit to MyContract, that is called SAM conversion.
4. When SAM conversion is and isn't allowed is messy.
a. As @Youssef Shoaib [MOD] mentioned, it works for argument positions.
b. It also works for some positions with explicit expected type (e.g., initializers): val a: MyContract = {}
i. But! It sometimes works only for lambdas and not for arbitrary expressions.
c. It does not work for callable reference inference.
d. It does not work "inside" type inference.
5. The rabbit hole continues in KT-84394.
My question to you @min would be -- what exactly are you trying to achieve?Marat Akhin
08/15/2026, 9:49 AMfun MyContract(impl: () -> Unit): MyContract), then basically yes. And yes, we do not actually generate them to the binary, they are generated at the call-sites (similarly to other SAM conversions).Marat Akhin
08/16/2026, 10:47 AMYoussef Shoaib [MOD]
08/16/2026, 12:17 PM(F) -> T, where F is the function type that converts to this lambda. This means you can't do T(T {}), which is probably for the better
fun main() {
T { } // works
T(T { }) // Argument type mismatch: actual type is 'T', but '() -> Unit' was expected.
}
fun interface T {
fun t()
}Marat Akhin
08/16/2026, 12:53 PMMarat Akhin
08/16/2026, 6:44 PMMarat Akhin
08/17/2026, 7:43 AM(T) -> T function on a value of type T with the intent to get that exact value back?Marat Akhin
08/17/2026, 8:46 AM<@U019AG342E5> described. And in general, the rule of thumb is "if in doubt, the compiler is usually right". Despite Kotlin having a specification, it is neither complete, nor sound, and the single main source of truth is the compiler behavior.
Your idea does make sense, and I quite like it, but it also makes code like MyContract(myContract) possible, which is (arguably) easy to write by mistake and which should be preferably avoided.