https://kotlinlang.org logo
#compiler
Title
# compiler
f

franztesca

10/10/2023, 8:51 PM
Are these errors expected? They seem quite counter-intuitive to me 😕 Playground: https://pl.kotl.in/w38CtHeCP
y

Youssef Shoaib [MOD]

10/10/2023, 8:58 PM
its because suspend here is actually a function which takes a lambda with no arguments. This works:
Copy code
fun main() {
    var myList: List<suspend (Int) -> Int>
    myList = listOf(suspend { n: Int -> n })
    myList = listOf(suspend { n: Int -> n})
    myList = listOf(suspend { it })
    //myList = listOf({ n: Int -> n })
    myList = listOf({ n -> n })
    myList = listOf({ it })
}

fun <T, R> suspend(block: suspend (T) -> R) = block
except that this convention is deprecated now, probably for the better, and so instead you can surround the lambda in parens, or better just rename that fun suspend to suspendLambda or something
thank you color 1
e

ephemient

10/10/2023, 9:21 PM
yep, even though it looks like a keyword it's actually https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/suspend.html
thank you color 1
this should be fixed in some future version of Kotlin https://youtrack.jetbrains.com/issue/KT-22765
f

franztesca

10/10/2023, 9:56 PM
Ahh, makes sense, thanks! What still may be confusing is why
Copy code
// Explicit type -> doesn't work
myList = listOf({ n: Int -> n })
// Implicit type -> works
myList = listOf({ n -> n })
🤔
2 Views