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!

Where Next?

Popular Pragmatic Bookshelf topics Top

jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New
mikecargal
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display) It looks like ​.with_simple_console_no_bg​(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex> product = %Product{} %Pento....
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
New
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1021 17094 374
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
AstonJ
Or looking forward to? :nerd_face:
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
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
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New

Sub Categories: