For pets/v2/src/main/kotlin/com/example/pets/Helpers.kt
(and in subsequent versions), I think it might be helpful to change the block
parameter name to something else.
suspend fun createWindow(
title: String,
block: suspend CoroutineScope.(JFrame) -> Unit
): Unit = withContext(Dispatchers.Main) {
Understandably in Kotlin block
is a common name for a function type parameter that is block of code to be executed (such as in the case of the Kotlin scope functions). But here I think the name can cause confusion due to the ambiguity with blocking code in the context of discussing Coroutines. Especially given it’s signature is block: suspend...
. In my case I saw its use before I saw its definition and started to think there was a new Coroutine block()
function we were about to learn about. Perhaps codeBlock
might be a better name , or create
? (create
makes more sense at the point its called in my humble opinion.)
Also, unless I am mistaken, this is the first time we have seen the suspend
key word as part of a function type parameter definition. So a couple of sentences highlighting that fact might be helpful. Like “hey did you notice we can define function type parameters as suspending functions?”
book-kotlin-coroutine-confidence version B3