Kotlin Coroutine Confidence: Change function name, maybe (pg 182)

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

I hadn’t considered that writing block right next to suspend could be confusing! It’s an interesting and very valid point. I think it’s very important to reduce opportunities for that kind of confusion when presenting code examples, so I’ll certainly consider changing this one. I’m tempted to go with something like builder or windowBuilder—I’ll give it some thought.

I also appreciate you pointing out that this might be the first time we’ve used a suspending lambda function as an argument to another function. I glossed over this code pretty quickly, since it’s mostly just there to simplify and enable the following code examples, but I agree, a quick note about that might be useful. Suspending lambdas are an important feature, after all.

Thank you for the feedback!

Either of those work well I think. :+1: As the saying goes, the hardest thing in software development is naming things.