Kotlin 新版本 1.9.0重要更新预览

释放 Kotlin 新版本 1.9.0 的强大功能

1. Kotlin K2编译器用于多平台

对K2编译器进行了进一步的改进,使其更加稳定。K2编译器针对JVM目标现已进入Beta版本,并且也可以在多平台项目中使用。

您可以通过将K2配置添加到项目的gradle.properties中,或者将其作为命令行参数传递给Gradle任务来尝试使用它。

// gradle.properties
kotlin.experimental.tryK2=true

// command line arguments
./gradlew build -Pkotlin.experimental.tryK2=true

注意:您可以在开发项目中尝试使用,但请不要在生产代码中使用,因为它仍处于Beta版本。

2. 语言功能和更新

Enum类中的entries属性

Enum类中引入了一个新的属性,称为entries,它将返回枚举常量的不可变列表。我们已经有了values()函数,它返回枚举常量的数组。那么它们之间有什么区别呢?嗯,values()函数返回一个新创建的、可变的枚举常量数组。另一方面,entries属性每次返回一个预分配的不可变枚举常量列表,这将减少性能开销。

enum class Vehicle {
    
    
    CAR, BIKE, TRUCK;
}

fun main(args: Array<String>) {
    
    
    val entries = Vehicle.entries
    val duplicate = Vehicle.entries
    println(entries == duplicate) // print true
    val values = Vehicle.values()
    val duplicateValues = Vehicle.values()
    println(values == duplicateValues) // print false as two array were created and it has differnt hashcode

    println(values[0]) // print CAR
    values[0] = Vehicle.TRUCK
    println(values[0]) // print TRUCK

    entries[0] = Vehicle.TRUCK // throw compile time errror
}

数据对象现在是稳定的

现在您可以在对象类中使用data关键字。您现在可以在对象中使用数据类的equalstoStringhashCode函数特性的所有优点。

支持内联构造函数上的辅助构造函数

您可以为内联类创建多个构造函数。当您希望在初始化内联类的底层属性时有一些逻辑时,这将非常有用。

@JvmInline
value class BMI(val value: Float) {
    
    

    constructor(heightInMetres: Float, weightInKg: Float) : this(weightInKg / (heightInMetres.pow(2F))) {
    
    

        check (heightInMetres == 0F) {
    
    
            "Height should be greater than 0"
        }


        check (weightInKg == 0F) {
    
    
            "Weight should be greater than 0"
        }
        
    }

}

3. 标准库更新

Kotlin Time API 现在是稳定的。将会有一篇即将发布的博文介绍如何使用 Kotlin 的新 Time API。
kotlin.io.path.Path 类引入了一个新的扩展函数 createParentDirectories()。调用此函数将创建所有未存在或未创建的父目录。我们可以利用此函数来摆脱递归检查文件夹是否存在以及文件夹创建逻辑的问题。
由于 Kotlin 1.9.0 目标 JVM 1.8,现在引入了正则表达式模式捕获组。您可以在此链接中查看详细的解释。
现在引入了一个名为 HexFormat 的新的实验性类。您可以使用它来存储十六进制数字,并且有可用于将 HexFormat 转换为字节数组、整数、长整型等的函数,反之亦然。
Volatile 注解现在适用于所有的多平台目标。它仅仅是一个标记注解,用于使变量/函数具有原子性(线程安全)。在 1.9.0 之前,它仅适用于 JVM。
现在引入了新的操作符 rangeUntil ( ..< ) 。您可以在自己的类中进行重写以实现自定义实现。

import java.text.SimpleDateFormat
import java.util.*

operator fun Date.rangeUntil(date: Date): Iterable<Date>  {
    
    
    val list = arrayListOf<Date>()
    val c = Calendar.getInstance()
    c.time = this
    while (date > c.time) {
    
    
        list.add(c.time)
        c.add(Calendar.DAY_OF_YEAR, 1)
    }

    return list
}

fun main() {
    
    
    val date1 = Date(1687372200000)
    val date2 = Date(1689140005851)

    val formatter = SimpleDateFormat("d MMM, yyyy")
    for (date in date1 ..< date2) {
    
    
        println(formatter.format(date))
    }
}

4. Kotlin / Native

Kotlin 1.9.0 引入了一个新的预览功能,即自定义内存分配器,以提高 Kotlin / Native 内存管理器的性能。当前的分配器对于垃圾回收来说并不高效。更多内容请查看下面的链接。

https://kotlinlang.org/docs/whatsnew19.html#preview-of-custom-memory-allocator

要启用此预览的自定义内存分配器功能,请在您的多平台项目的 build.gradle 中添加以下内容。

kotlin {
    
    
  macosX64("native") {
    
    
    binaries.executable()

    compilation.configureEach {
    
    
      compilationOptions.configure 
        freeCompilerArgs.add(-Xallocator=custom)
      }
    }
  }
}

猜你喜欢

转载自blog.csdn.net/u011897062/article/details/131873417
今日推荐