Joshua Hansen
12/20/2023, 11:54 PMclass foo(
var bar: String? = null,
var baz: Int? = null,
companion object {
fun buildFoo(block: Foo.() -> Unit): Foo { // not suspending
return Foo().apply { block() }
}
}
)
suspend fun newFoo(): Foo = coroutineScope {
val bar = async { runDbQueryToGetBar() }
val baz = async { runDbQueryToGetBaz() }
// Send off the queries together and await them both
val results = awaitAll(bar, baz) // List<Any> - awkward?
buildFoo { // this: Foo
this.bar = results[0] as String
this.baz = results[1] as Int
} // returns from coroutineScope
}
Joshua Hansen
12/20/2023, 11:57 PMMap<String, Int>
and it gives me an Unchecked Cast warning, which is just a little annoying.kevin.cianfarini
12/21/2023, 12:13 AMclass foo(
var bar: String? = null,
var baz: Int? = null,
companion object {
fun buildFoo(block: Foo.() -> Unit): Foo { // not suspending
return Foo().apply { block() }
}
}
)
suspend fun newFoo(): Foo = coroutineScope {
val bar = async { runDbQueryToGetBar() }
val baz = async { runDbQueryToGetBaz() }
buildFoo { // this: Foo
this.bar = bar.await()
this.baz = baz.await()
} // returns from coroutineScope
}
Your usage of awaitAll is probably pointless here so it's better just to await where you need the values.asdf asdf
12/21/2023, 12:54 AMbuildFoo
an inline
function, you would be able to suspend inside of the lambda without actually marking buildFoo
as suspendingmkrussel
12/21/2023, 1:27 AMbuildFoo
.