Kotlin 1.9.0 officially released

Kotlin version 1.9.0 has been officially released and the K2 compiler for the JVM is now in beta. This release includes new language features and improvements for Kotlin Multiplatform and Kotlin/Native.

Here are the key takeaways from this release:

New Kotlin K2 compiler update

The Kotlin team continues to stabilize the K2 compiler, and version 1.9.0 introduces further improvements. The K2 compiler for the JVM is currently in Beta.

Basic support for Kotlin/Native and multi-platform projects is now provided.

Stable replacement for enum class valued functions

In 1.8.20,  entries properties on enum classes were introduced as an experimental feature.  entries Properties are  values() a modern and performant replacement for functions.

In 1.9.0,  entries properties have been stabilized. Although the values() function is still supported, the official recommendation is to use the entries attribute instead.

enum class Color(val colorName: String, val rgb: String) {
    RED("Red", "#FF0000"),
    ORANGE("Orange", "#FF7F00"),
    YELLOW("Yellow", "#FFFF00")
}

fun findByRgb(rgb: String): Color? = Color.entries.find { it.rgb == rgb }

Stable ..< operator for open ranges

New operators for open ranges  ..< were introduced in Kotlin 1.7.20 and became stable in 1.8.0. In 1.9.0, the standard library API for working with open ranges has also been stabilized.

New  ..< operators make it easier to understand when declaring open-ended ranges. If you use  until a function, it can easily be misinterpreted as including an upper bound.

Here is  until an example of using the function:

fun main() {
    for (number in 2 until 10) {
        if (number % 2 == 0) {
            print("$number ")
        }
    }
    // 2 4 6 8
}

Here's an example using the new  ..< operators:

fun main() {
    for (number in 2..<10) {
        if (number % 2 == 0) {
            print("$number ")
        }
    }
    // 2 4 6 8
}

New generic function: get regex capture group by name

In 1.9.0 there is a general function  groups that can be used to retrieve the contents of a group by a name matched by a regular expression. This is useful when you want to access the regex match results belonging to a specific capture group.

The following is an example of a regular expression with three capture groups:  city , ,  state and  areaCode . Matching values ​​can be accessed using these group names:

fun main() {
    val regex = """\b(?<city>[A-Za-z\s]+),\s(?<state>[A-Z]{2}):\s(?<areaCode>[0-9]{3})\b""".toRegex()
    val input = "Coordinates: Austin, TX: 123"

    val match = regex.find(input)!!
    println(match.groups["city"]?.value)
    // Austin
    println(match.groups["state"]?.value)
    // TX
    println(match.groups["areaCode"]?.value)
    // 123
}

New path utility for creating parent directories

In 1.9.0, there is a new extension function  createParentDirectories() that can be used to create a new file with all required parent directories.

createParentDirectories() Especially useful when copying files. For example, you can  copyToRecursively() use it with a function:

sourcePath.copyToRecursively(
   destinationPath.createParentDirectories(),
   followLinks = false
)

Gradle Configuration Cache Preview in Kotlin Multiplatform

Kotlin 1.9.0 supports Gradle configuration caching in multiplatform libraries, and library authors can already benefit from improved build performance. The Gradle configuration cache speeds up the build process by reusing the results of the configure phase in subsequent builds. This feature has been stabilized since Gradle 8.1.

Note: The Kotlin Multiplatform plugin still does not support Gradle configuration caching with Xcode integration tasks or the Kotlin CocoaPods Gradle plugin.

Changes to Android targets in Kotlin Multiplatform

In the future, Google's Android team will provide its own Gradle plugin to support Android in Kotlin multiplatform.  To make room for Google, blocks  in the current Kotlin DSL were renamed in 1.9.0  ,  changing all occurrences of blocks in build scripts to   .androidandroidandroidTarget

This is a temporary change, just for releasing the name of the upcoming DSL from Google  android , and subsequent  Google's plug-ins will be the preferred way to use Android in multi-platform projects.

Custom memory allocators in Kotlin/Native (preview)

Kotlin 1.9.0 introduces a preview of the custom memory allocator, an allocation system that improves the runtime performance of the Kotlin/Native memory manager. It divides system memory into pages, allowing independent cleaning in sequential order. Each allocation becomes a block of memory within a page, and the page keeps track of the block size.

The new allocator allows to have multiple independent allocations at the same time, allowing the Kotlin team to experiment with different page layouts to further improve performance.

Library linking in Kotlin/Native

Starting with Kotlin 1.9.0, the Kotlin/Native compiler handles linking in Kotlin libraries in the same way as Kotlin/JVM.

The build will not fail during compilation if there are linking issues between third-party Kotlin libraries. Instead, these errors are encountered only at runtime, just like on the JVM.

The Kotlin/Native compiler will report a warning every time it detects a library linking problem. You can find such warnings in compile logs, for example:

No function found for symbol 'org.samples/MyRemovedClass.doSomething|3657632771909858561[0]'

Can not get instance of singleton 'MyEnumClass.REMOVED_ENTRY': No enum entry found for symbol 'org.samples/MyEnumClass.REMOVED_ENTRY|null[0]'

Function 'getMyRemovedClass' can not be called: Function uses unlinked class symbol 'org.samples/MyRemovedClass|null[0]'

File size related optimizations in Kotlin/Wasm

Kotlin 1.9.0 introduces significant file size improvements for WebAssembly (Wasm) projects. Compared to the "Hello World" project, the code footprint of Wasm in Kotlin 1.9.0 is now more than 10 times smaller than in Kotlin 1.8.20.

These size optimizations can improve resource utilization and improve performance when targeting the Wasm platform with Kotlin code.

 

For a complete list of changes, see What's New in Kotlin 1.9.0 or the release notes on GitHub .

Guess you like

Origin www.oschina.net/news/248354/kotlin-1-9-0-released