louiscad
04/20/2023, 10:40 AMb() produces more complex bytecode than a(), instead of just adding a private field to the anonymous class?
fun interface Counter {
fun getAndIncrement(): Int
}
fun a(): Counter = object : Counter {
var number = 42
override fun getAndIncrement(): Int {
return number++
}
}
fun b(): Counter {
var number = 42
return Counter { number++ }
}simon.vergauwen
04/20/2023, 10:48 AMb() you’re not defining an anonymous class, but it’s just lambda definition of the single abstract method.
So the compiler needs to treat it like a variable capture that could be coming from anywhere.simon.vergauwen
04/20/2023, 10:49 AMlouiscad
04/20/2023, 10:49 AMsimon.vergauwen
04/20/2023, 10:50 AMlouiscad
04/20/2023, 10:50 AMlouiscad
04/20/2023, 10:50 AMsimon.vergauwen
04/20/2023, 10:52 AMsimon.vergauwen
04/20/2023, 10:53 AMlouiscad
04/20/2023, 10:55 AMlouiscad
04/20/2023, 10:56 AMsimon.vergauwen
04/20/2023, 10:58 AMYoussef Shoaib [MOD]
04/20/2023, 12:03 PMshikasd
04/20/2023, 12:41 PMvar, I believe. You can potentially use the variable outside of captured scope (e.g. in b body), so compiler does the simplest thingYoussef Shoaib [MOD]
04/20/2023, 12:42 PMshikasd
04/20/2023, 12:44 PM