is this the most elegant / proper way to use e.g. ...
# announcements
p
is this the most elegant / proper way to use e.g. repeat() with a suspending function?
Copy code
suspend fun bla(): Unit = TODO()

repeat(10) {
  runBlocking {
    bla()
  }
}

or do I need a suspendable version of repeat ("repeatAsync") ?
m
The context is missing. Will you call
repeat
inside a
suspend fun
or a regular
fun
? Big difference 🙂
p
inside a regular function
e
as I said in the other thread, inline functions can make use of suspend lambdas
repeat is an inline function
p
that’s the point I was missing
makes so much of a difference
I didn’t expect inline to do so much more
m
So if it’s supposed to block then your example is just fine. You can also make your own function that does both
repeat
and
runBlocking
for having to write less here. But
repeatAsync
would be a bad name for that because it’s not asynchronous.
e
if you imagine it as "copy and paste this into the caller" then it makes sense that suspension should work 🙂
(there's a bit of compiler cleverness to make it happen, but still)
just to be clear,
Copy code
suspend fun bla(): Unit = TODO()

runBlocking {
    repeat(10) {
        bla()
    }
}
works
p
thank you guys, telling me about inline behavior and suspend was the missing building block for me]
I saw it solely as an performance related thing so far
I converted a java team in a bank to kotlin and we really love it
it’s just a far better option
m
Don’t overthink performance 😄 First make it work great. Then go deep if there are actually problems.
➕ 1
p
I didn’t use inline at all, it’s just to be compatible with suspending / non-suspending functions
a legit use I guess
I generally follow the rule “make it work, right, fast”
m
You may have used inline functions already without noticing 🙂 In most cases they’re used just like regular functions.
p
apply
using it all the time
m
yup