Why can't this work: ``` fun bar(fn: suspend () -&...
# announcements
l
Why can't this work:
Copy code
fun bar(fn: suspend () -> Unit ) { }
fun far() {  
   val fn = {} // () -> Unit
   bar(fn)
}
While I can easily do:
Copy code
fun far() {
  val fn = {}
  bar {  fn() }
}
s
Suspend functions can call both normal functions and suspend functions. That being said, suspend functions cannot be substituted for normal functions, like in your example; this is because the signature of a
suspend {}
is not the same as the signature of a regular
{}
.
l
Hm... So perhaps inside
bar
I would do something with
fn
that required the suspension?
That's why it's disallowed?
s
It's actually disallowed because suspend functions have an implicit
Continuation
parameter
If you change `fn`’s declaration to
suspend {}
, it’ll work
Copy code
class Foo {
    suspend fun suspended() {
        println("h")
	}
    
    fun blocking() {
        println("z")
    }
}

/**
 * We declare a package-level function main which returns Unit and takes
 * an Array of strings as a parameter. Note that semicolons are optional.
 */
fun main(args: Array<String>) {
    val parameters = Foo::class.java
        .declaredMethods
    	.associate { it.name to it.parameters.map { it.type.name } }
        .toMap()
    println(parameters)
}
if you run this, it'll output
{blocking=[], suspended=[kotlin.coroutines.Continuation]}
l
I see