Kotlin 2.10 programming language extends connection to iOS and WebAssembly
In addition to minor syntactic additions, Kotlin 2.10 brings innovations to the K2 compiler, the WebAssembly toolchain and for interaction with Swift.
(Image: iX)
Six months after Kotlin 2.0, Kotlin 2.10 has now been released as the first minor release, which expands the K2 compiler, among other things. There is also a direct connection to Apple's Swift, and the toolchain for WebAssembly can compile incrementally.
The K2 compiler introduced as a beta in Kotlin 1.9.20 and marked as stable in Kotlin 2.0 also receives additional compiler checks in the current release. JetBrains has also made some improvements to the kapt plug-in (Kotlin Annotation Processing Tool) for K2, which still has alpha status.
Videos by heise
More ifs, more $$ and flexible inline exit
Kotlin 2.10 introduces three syntactic changes, all of which are marked as preview and require explicit activation via compiler flags. With Guarding Conditions, when blocks can be extended with additional conditions added with if for the individual branches. The blocks can mix branches with and without guarding conditions. An example can be found in the Kotlin documentation:
sealed interface Animal {
data class Cat(val mouseHunter: Boolean) : Animal {
fun feedCat() {}
}
data class Dog(val breed: String) : Animal {
fun feedDog() {}
}
}
fun feedAnimal(animal: Animal) {
when (animal) {
// Branch with only the primary condition.
// Returns `feedDog()` when `Animal` is `Dog`
is Animal.Dog -> animal.feedDog()
// Branch with both primary and guard conditions.
// Returns `feedCat()` when `Animal` is `Cat` and is not `mouseHunter`
is Animal.Cat if !animal.mouseHunter -> animal.feedCat()
// Returns "Unknown animal" if none of the above conditions match
else -> println("Unknown animal")
}
}
There is also an addition for inline functions. Non-local exit via continue and break is now also permitted there. Previously, the exit from a lambda outside the embedding function was only permitted via return.
There is a small convenience improvement when dealing with the $ character in string literals. Since it indicates the string interpolation, the use of the dollar sign within the literal had to be done via the construct ${'$'}. Kotlin 2.10 now introduces multi-string interpolation: The number of dollar signs before an interpolated string indicates how many characters trigger a string interpolation. The following example shows the procedure using a JSON schema:
val KClass<*>.jsonSchema : String
get() = $$"""
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/product.schema.json",
"$dynamicAnchor": "meta"
"title": "$${simpleName ?: qualifiedName ?: "unknown"}",
"type": "object"
}
"""
WebAssembly and Swift
When creating WebAssembly applications, the Kotlin/wasm toolchain now offers incremental compilation to avoid having to recompile the entire code for every change.
In the roadmap for cross-platform development published at the end of 2023, JetBrains had better iOS connectivity at the top of the list, and Kotlin 2.10 takes a big step forward in two areas.
Applications that combine Kotlin and Swift, for example in cross-platform development with integrated native code in Swift, previously required a detour via Objective-C. JetBrains is introducing a Swift export with the current release, but this is still marked as experimental.
There is also an improvement elsewhere in the interaction with Apple's ecosystem: the iosArm64 platform is now in the top category (Tier 1) of Kotlin/Native targets.
Further new features, including the interaction with Gradle, can be found on the Kotlin blog.
(rme)