Javaru

Javaru

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

Marked As Solved

sam-cooper

sam-cooper

Author of Kotlin Coroutine Confidence

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!

Popular Pragmatic Bookshelf topics Top

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=> (create-...
New
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “>...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New

Other popular topics Top

dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New