I need a help in understanding lambda function let...
# getting-started
t
I need a help in understanding lambda function let's say we have code snippet mentioned below
Copy code
init {
    itemView.setOnClickListener {
        onItemClicked(bindingAdapterPosition)
    }
}
will it evaluate the value of
bindingAdapterPosition
at the time of calling the
init{}
or when a click happens. Moreover would love to know how actually lambda works deep down
c
This is about Java, but except for the different syntax, they work exactly the same way in Kotlin: https://www.digitalocean.com/community/tutorials/how-to-use-lambdas-in-java
๐Ÿ‘ 1
m
You can also convert to byte code and then decompile it. Should show you how it gets converted to an anonymous class.
๐Ÿ™Œ 1
t
@Michael Krussel can you help me how can i do it. Secondly does it decompiles to java ? If it does, then what the purpose of decompiling to java rather than kotlin, isn't kotlin independent of java (except jvm)?
m
I think it decompiles to Java because that is a closer language to the JVM than Kotlin. Similar to decompiling assemble to C. But it is also helpful for seeing how the Kotlin code advance features translate back out. How to do it is
Tools -> Kotlin -> Show Kotlin Byte Code
.
t
@Michael Krussel I see, just a follow up doubt like i saw people explaining optimization based on that decompiled java code, but shouldn't using bytecode rather than java code is a correct way, as Kotlin might have it's different way of getting to the same bytecode
c
Should show you how it gets converted to an anonymous class.
That's only true if you use Java 6 or 7. If you target later versions, it should use proper Java lambdas.
๐Ÿ‘ 1
m
The tool first shows the byte code and you can look at that if you know byte code, but most people want to see a decompiled version.
I wasn't sure if Java lambdas get converted to anonymous classes or not. Not really a JVM export.
๐Ÿ‘ 1
c
If it does, then what the purpose of decompiling to java rather than kotlin, isn't kotlin independent of java (except jvm)?
Yes, it's just that IntelliJ has been able to decompile any bytecode to Java for literal decades now. Since decompiling is just about understanding what some code does, there's not much need for a Kotlin decompiler (and it's hard to write one!) so IntellIiJ just has a Java decompiler
If you want to go more in depth, there's the "Kotlin FIR tree" plugin that prints the actual internal representation of the compiler, but honestly it's very hard to understand: https://plugins.jetbrains.com/plugin/23270-fir-tree
t
@CLOVIS Thanks, will try ๐Ÿ™Œ
c
Honestly I recommend to use the JVM decompiler as @Michael Krussel said, it will be much, much easier to read
๐Ÿ‘ 1
w
I also wanna comment that using the Kotlin to Java decompiler might not always exactly generate Java code that would compile back down to the exact same bytecode. In short, there might be some details lost in translation. While it's a great tool for most use cases like this, if you ever truly need to understand specifics about how it compiles, you'd need to look at the JVM bytecode.
๐Ÿ‘ 1