Not sure what I'm missing here. ```doSomething() ...
# coroutines
c
Not sure what I'm missing here.
Copy code
doSomething() <=== Works!
bufferedReader.forEachLine {
  doSomething() <=== Doesn't work (Error: "Suspension functions can be called only within coroutine body")
}
what about a bufferedReader.forEachLine kicks me out of my coroutine body...? hm
j
forEachLine
is not an inline function, which is why the lambda is no longer executing in the coroutine scope
maybe this is something you should report on YouTrack, I don't know if there is any reason for this function to not be an inline one
c
Interesting! I did
Copy code
while (bufferedReader.readLine().also { currentLine = it } != null) {
and now everything works! Thanks
👍 1
I'll try to follow a bug on youtrack at some point. lol
^demonstrates how to convert a buffered reader to a flow of lines which can easily be consumed with suspend functions
a
although opening the file should be part of the flow and only executed on Dispatchers.IO when the flow is collected, not when File.lineFlow() is called
so more like:
Copy code
fun File.lineFlow(): Flow<String> = flow {
    bufferedReader().useLines { lines ->
        emitAll(lines.asFlow())
    }
}.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
s
Looks like this has been discussed in https://youtrack.jetbrains.com/issue/KT-17192
forEachLine
is mentioned in the comments
👍 1