How to call joinToString with suspending function

This is a toy example of what i want to accomplish:

import kotlinx.coroutines.delay

suspend fun x() {
    val names = listOf("Abe", "David", "Boris")
    names.map { toUpper() }.joinToString()
}

suspend fun toUpper(name: String): String {
    delay(100)
    return name.toUpperCase()
}

names.map { toUpper() }.joinToString() is not nice and android studio says that it can be simplified to

names.joinToString { toUpper() }

Unfortunately this does not work because now there is an error:

Suspension function can just be called in coroutines body

I guess that is because joinToString is not inlined.

Is there a way to overcome this?

I think this is probably a bug in the Kotlin plug-in. It shouldn’t recommend the simplification.

1 Like

You are right. After searching in the issue tracker of the android studio kotlin plugin i found this issue:
https://youtrack.jetbrains.com/issue/KT-23934
Thanks.