https://kotlinlang.org logo
#announcements
Title
# announcements
w

william

06/13/2019, 9:25 PM
if i have a few functions that have a default argument of
onComplete: () -> Unit = {}
would it be best to put this no-op function in a companion object somewhere or does kotlin automatically do any optimizations ?
k

karelpeeters

06/13/2019, 10:20 PM
A quick look at the generated bytecode for
Copy code
fun foo(block: () -> Unit) {
    block()
}

fun main() {
    foo {}
    foo {}
}
shows that there's not much optimization happening here, two separate classes are created to represent the
{}
. You can try looking at your exact case too.
w

william

06/13/2019, 11:16 PM
@karelpeeters how do you look at the generated bytecode like that?
k

karelpeeters

06/13/2019, 11:25 PM
Tools > Kotlin > Show Kotlin Bytecode (> Decompile, kind of buggy)
👍 2
2 Views