It seems to me that Im able to call a suspend func...
# coroutines
z
It seems to me that Im able to call a suspend function in a non-suspending block. Why is that? Details in thread 🧵
*This results in
'*Suspension functions can be called only within coroutine body'
which is expected.
Copy code
final override suspend fun matching(
    specification: S,
): List<T> {
    return execute {
        val ids = matching(specification).executeAsList()
        cache.get(ids) //suspend call
    }
}
But this does not.
Copy code
final override suspend fun all(): List<T> {
    return execute {
        all()
            .executeAsList()
            .chunked(MAX_QUERY_SIZE)
            .flatMap { ids -> cache.get(ids) } //suspend call
    }
}
The execute function used in both places is this:
Copy code
private suspend inline fun <R> execute(
    crossinline action: ProgressionDatabase.() -> R,
): R {
    return withContext(IO) {
        val database = store.database()
        database.action()
    }
}
I cant tell why its possible to call the suspend function in one place, but not the other.
e
inline
is designed to allow for inlining a suspend lambda into a suspend function

https://youtu.be/R2395u7SdcIâ–¾

z
Im 100% with you on what he mentions in the video. From my understanding, crossinline changes this behavior slightly in that Id need a
suspend
declaration before the lambda in order for it to be able to call other suspend functions? And in either case really, I still dont understand why one execute block is able to call a suspend function, whereas the other is not?
e
unclear what
executeAsList()
does, but if it returns a
Sequence
then its operators are not inlined
z
Its part of SqlDelight:
Copy code
fun executeAsList(): List<RowType> {
  val result = mutableListOf<RowType>()
  execute().use {
    while (it.next()) result.add(mapper(it))
  }
  return result
}
The compiler is now warning me in both cases as expected.. 🤔 Ive been able to compile and run the code for over 4 hours prior to this, lol. Im guessing its a bug in the IDE.
e
yeah if that's the return type then it doesn't make sense for them to behave differently. did you confirm either of them with the command-line compiler?
z
I agree. Confirm with the command line compiler -> Do you mean if it compiles at all? It did compile all this time, I never thought to check through the command line but I suspect that it wouldve failed identical to how it now fails as expected.
e
right. mostly I was just wondering, since that might point out some possible problems in the IDEA plugin. but if it seems to be behaving as expected now, there's no real need to check
z
Although not directly related to this as far as I can tell, I did have a "crash" in the IDE about 5 hours ago. Hard to tell if thats what eventually lead to this, but Ive reported it (Im on the latest canary release of Android Studio as well). Thanks for the dicussions blob smile happy
134 Views