This post summarizes my presentation for the Android Developer Peru meetup in July 2023. The topic is the new Kotlin compiler K2 and the changes that we should take into account for the development of apps in the future.
Published on November 03, 2023 by Daniel Peñaloza
kotlin kotlin compiler k2
1 min READ
For the entire presentation, you can watch it here (It is in Spanish):
To prepare for the new K2 compiler and how it will affect our current projects we have to understand how it works inside and what specific things are changing with the new compiler.
A compiler is a computer program that transforms source code into machine code. In particular, the Kotlin language can convert kt files into JVM bytecode, Javascript, or LVVM bytecode.
When we are talking about the Kotlin compiler, we can divide the work it does into two phases the Frontend and the Backend
The Frontend is in charge of converting the source code into two different structures: the syntax tree and the semantic info
The syntax tree organize the elements of the source code to represent the relationships between the variables and the keywords. At this moment in time, the variables don’t know what types are them, the only important part is the relationship between all the elements. The relationships follow a structure that is already predifined. It is like writing a sentence where there are some predifined grammar rules. In fact it is called grammer. You can find all of them here: Kotlin Grammar
The semantic info is a table where each row has an id and specific information related to that like for example what type of value is inside this variable, or from where this function comes from or from which package this function comes from.