https://kotlinlang.org logo
Title
k

karelpeeters

09/09/2018, 9:35 PM
Too bad the optimization isn't that smart:
var mainCount = 0
mainCount++

thread {
    while( true ) {
        println(mainCount)
        Thread.sleep(1000)
    }
}
could work without reference too, but the compiler generates it anyway.
s

Sam

09/09/2018, 9:38 PM
Thx, how would it work without reference?
fun testLambda( block : () -> Unit ) { thread { while( true ) { block() Thread.sleep( 1000 ) } } }
k

karelpeeters

09/09/2018, 9:38 PM
Well in my example
mainCount
isn't modified after it "leaks", so no reference is needed.
s

Sam

09/09/2018, 9:39 PM
If mainCount was passed by value to the generated class, how would the generated class see the latest value?
Oh ok, got it