The latest release of Kotlin introduces a number of new language features, including the ..<
operator for open ranges, extended regular expressions, and more. Additionally, it brings improvements to both Kotlin Multiplatform and Kotlin/Native.
Kotlin 1.9 stabilizes the synthetic entries
property associated to enum classes, which returns a list of all values of defined enum constants. entries
deprecates values()
and provide better performance by using a pre-allocated list of values instead of allocating a new array each time.
Another minor language feature is the new ..<
operator to express open-ended ranges. This new syntax, says JetBrains, makes it clearer that the upper bound is not included.
Regular expressions have become more flexible in Kotlin 1.9 thanks to a new group
function that can be used to retrieve a regex group content by name. For example, you can define the following regex with a few named groups:
val regex = """\b(?<city>[A-Za-z\s]+),\s(?<state>[A-Z]{2}):\s(?<areaCode>[0-9]{3})\b""".toRegex()
You can then use group names to access matched values:
val match = regex.find(input)!!
println(match.groups["city"]?.value)
// Austin
println(match.groups["state"]?.value)
// TX
println(match.groups["areaCode"]?.value)
Kotlin 1.9 also improves support for Kotlin/Native and Kotlin Multiplatform.
In Kotlin/Native, you can now preview custom memory allocators, which aims to improve the runtime performance of the Kotlin/Native memory manager. Custom allocators divide the system memory into pages, allowing independent sweeping in consecutive order. Custom memory allocators should be enabled using the -Xallocator=custom
compiler option.
Objective-C and Swift interoperability have also been improved thanks to a new policy for Objective-C/Swift object deallocation, which now happens on the main thread when appropriate, thus reducing the chances for memory leaks.
Other new features in Kotlin 1.9 are the ability to configure standalone iOS mode for iOS simulator tests and unified handling of linkage issues across Kotlin JVM and Kotlin/Native. In particular, builds will not fail anymore when there are linkage issues.
Moving to Kotlin Multiplaform, it includes the possibility to preview the Gradle configuration cache and changes to Android target support, paving the way for a new Google-developed Gradle plugin.
As a last remark about Kotlin 1.9, it is worth mentioning that it includes a beta version fo the new K2 compiler for the JVM, which aims to provide better performance, speed up language feature development, and provide a better architecture for multiplaftorm objects. K2 will become the default stable compiler in Kotlin 2.0.
For a full list of changes in Kotlin 1.9, read the official release notes.