https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

rafal

07/11/2017, 2:44 PM
Guys, do you know whether theres is a way to tell kotlin compiler NOT to optimise some `var`s? Here's my example on JVM (#coroutines) :
Copy code
fun <T> ReceiveChannel<T>.reduce(reducer: (T, T) -> T) = produce<T>(Unconfined) {
    var prev: T? = null
    for (elem in this@reduce) {
        if (prev == null) {
            prev = elem
            send(elem)
        } else {
            val new = reducer(prev, elem)
            prev = new
            send(new)
        }
    }
}
When run in debug mode everything works fine. However when run normally reading from
prev
always returns
null
- I suppose some "optimisation" is taking place in non-debug builds.