Kotlin 2.0.0
Introduction
With the release of Kotlin 2.0.0 a lot of new tech had been uncovered and has become stable for devs to to use.
I was especially excited, as I wanted to see what new features Kotlin was getting. I was hoping to see if Jetpack Compose would be mentioned one way or another in this release and if so in what capacity.
I will be covering items that have made me feel excited!
Key Points
- Point 1: Kotlin K2 Compiler
- Strong benchmarks for build times
- Language feature improvements
- Impactful smart cast improvements
- Point 2: New Compose Compiler Plugin
- New Compose compiler Gradle plugin
- Point 3: Experimental Power-Assert Compiler Plugin
- This will help with unit testing
- Point 4: Extensible Data Arguments
- Although coming in 2.2.0 is worthy of mentioning
- Point 5: Guarded Conditions
- Will help with
when
conditions
- Will help with
- Point 6: Context-Sensitive Resolution
- Will eliminate the need to reference the base class for
enum
andsealed
classes inwhen
conditions
- Will eliminate the need to reference the base class for
Kotlin K2 Compiler
-
With the release of Kotlin 2.0.0, the new Kotlin K2 compiler is used by default and is stable for all target platforms.
-
The K2 compiler brings major performance improvements, speeds up new language feature development, unifies all platforms that Kotlin supports, and provides better architecture for Multiplatform Projects.
-
The K2 compiler introduces extremely fast build times. All comparisons were done with version 1.9.23.
- Up to 94% compilation speed gains for clean builds
- Up to 488% faster on initialization phase
- Up to 376% faster in the analysis phase
-
To get the full K2 experience use the K2 Mode plugin
- Using K2 mode on IntelliJ and Android Studio you have to be on version 2024.2.
-
Smartcast improvements:
- In Kotlin 2.0.0, if you combine type checks for objects with an
or
operator (||
), a smart cast is made to their closest common supertype. Before this change, a smart cast was always made to theAny
type. - Previously if you had a nullable variable type you would only be able to smartcast it within the scope of the
if
expression which evaluated it to notnull
. This information would not be spread outside of that scope. From Kotlin 2.0.0, if you declare a variable before using it in yourif
,when
, orwhile
condition, then any information collected by the compiler about the variable will be accessible in the corresponding block for smart-casting.
- In Kotlin 2.0.0, if you combine type checks for objects with an
New Compose Compiler Plugin
- The Jetpack Compose compiler, that converts
composables
to Kotlin code, has now been merged into the Kotlin repository.- This will help transition Compose projects to Kotlin 2.0.0, as the compiler will always ship with Kotlin.
- This also bumps the Compose compiler to version 2.0.0.
Experimental Power-Assert Compiler Plugin
- A testing plugin that generates failure messages that include intermediate values of the assertion expression.
- When an assertion fails in a test, the improved error message shows the values of all the variables and subexpressions within the assertion.
- This makes it clear which part of the condition causes failure.
- By default, it transforms
assert()
function calls but can also transform other functions, such asrequire()
,check()
, andassertTrue()
. - Example error message provided by the plugin
- Since it’s experimental you will have to use this annotation
@OptIn(ExperimentalKotlinGradlePluginApi::class)
For more information about Power Assert
Extensible Data Arguments
- Though is not confirmed, is a pretty exciting potential new addition to Kotlin.
dataarg
similar tovarargs
but for data classes.
Guarded Conditions
- Used to help us check multiple conditions within a
when
block without repeating the variable name. - Also allows for
if
conditions on to the branches ofwhen
statments. - Previous implementation:
- Kotlin 2.0.0
Context-Sensitive Resolution
- This is coming out in Kotlin 2.2.0.
- This adds on to what we previously discussed, by no longer requiring you to add the base class to your
sealed
types orenums
. - So going of the previous example we can completely remove the
SearchPanel
base class as follows.
Conclusion
- Excluding speculated features like
dataargs
, these additions will help cut down on build times, add cleanliness to our code, and remove pain points that were there previously. - I am very excited to try these out in my projects, as the highlighted features code issues that I have come across while trying to write clean and concise code.