I have a problem with non-local variables in closu...
# announcements
n
I have a problem with non-local variables in closures. I need to read through a (potentially huge) log file line by line and untangle it by adding lines to a varying set of StringBuilders based on an id in the line. In addition, some lines are continuation lines which should be added to the last used StringBuilder. I thus have a
val writers = mutableMapOf<String, StringBuilder>()
and a
val current: StringBuilder? = null
. I first then used
for (line in reader.readLines()) { ... }
which works but reads the whole file into memory. However,
reader.forEachLine { line -> ... }
does not work. So what is the Kotlin way of doing this?