Jakob Heher
12/01/2025, 1:36 PMfun fnTakingUnitLambda(fn: ()->Unit) = ...
// this is legal, and i want it not to be:
fnTakingUnitLambda { 42 }
// this should still be legal:
fnTakingUnitLambda { Unit }
// this should also work:
fnTakingUnitLambda { someOtherFnThatReturnsUnit() }Youssef Shoaib [MOD]
12/01/2025, 1:37 PMJakob Heher
12/01/2025, 1:39 PMResult<Unit>-returning functions extremely misuse-prone, since simply using the "wrong" map function (map instead of transform) will cause the error to be quietly swallowedYoussef Shoaib [MOD]
12/01/2025, 1:42 PM@OnlyInputTypes that forces the compiler to ignore info from expected types. You can access them by redefining them in your own code, or with some error suppressions (but that's unrecommended)Youssef Shoaib [MOD]
12/01/2025, 1:42 PMJakob Heher
12/01/2025, 2:19 PMmap with @OnlyInputTypes and the code above still compiles, strangely enoughJakob Heher
12/01/2025, 2:22 PMJakob Heher
12/01/2025, 2:23 PMYoussef Shoaib [MOD]
12/01/2025, 2:24 PM@NoInfer works:
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
fun <T> fnReturningBlockType(fn: ()->T): @kotlin.internal.NoInfer T = fn()
fun main() {
val x: Unit = fnReturningBlockType { 42 } // Initializer type mismatch: expected 'kotlin.Unit', actual '<http://kotlin.Int|kotlin.Int>'.
}Youssef Shoaib [MOD]
12/01/2025, 2:26 PMJakob Heher
12/01/2025, 2:27 PMJakob Heher
12/01/2025, 2:28 PMResult has out variance the ignoring should also not cause complications (we'll still test this)Jakob Heher
12/02/2025, 3:12 PM@NoInfer behaves as expected (it prevents the compiler from using the explicit result type as a hint that it should coerce to Unit) in kotlin 2.1.20Jakob Heher
12/02/2025, 3:12 PMJakob Heher
12/02/2025, 3:16 PMJakob Heher
12/02/2025, 3:17 PMInitializer type mismatchJakob Heher
12/02/2025, 3:17 PMJakob Heher
12/02/2025, 3:27 PM