Kotlin:2.0.20 的新特性

一、概述

Kotlin 2.0.20英文版官方文档

Kotlin 2.0.20发布了!这个版本包括对Kotlin 2.0.0的性能改进和bug修复,我们在其中宣布Kotlin K2编译器为Stable。以下是本次发布的一些亮点:

二、数据类复制函数与构造函数具有相同的可见性

Kotlin 2.0.20开始引入更改,以提高数据类的一致性,并替换实验性上下文接收器特性。
目前,如果使用私有构造函数创建数据类,则自动生成的copy()函数不具有相同的可见性。这可能会在稍后的代码中导致问题。在未来的Kotlin版本中,我们将引入copy()函数的默认可见性与构造函数相同的行为。此更改将逐步引入,以帮助您尽可能顺利地迁移代码。

我们的迁移计划从Kotlin 2.0.20开始,它会在代码中发出警告,将来可见性会发生变化。例如:

复制代码
// Triggers a warning in 2.0.20
data class PositiveInteger private constructor(val number: Int) {
    companion object {
        fun create(number: Int): PositiveInteger? = if (number > 0) PositiveInteger(number) else null
    }
}

/**
 *  数据类复制函数与构造函数具有相同的可见性
 *  
 *  @ConsistentCopyVisibility 用于标记一个类或接口,以表明其副本在可见性方面应该是一致的。换句话说,当你创建一个类的副本时,副本的可见性应该与原始类实例的可见性相匹配。
 */
fun testConsistentCopyVisibility(){
    val positiveNumber = PositiveInteger.create(42) ?: return
    // Triggers a warning in 2.0.20
    val negativeNumber = positiveNumber.copy(number = -1)
    // Warning: Non-public primary constructor is exposed via the generated 'copy()' method of the 'data' class.
    // The generated 'copy()' will change its visibility in future releases.
}


fun main() {
  testConsistentCopyVisibility()
}

运行结果

报错信息

复制代码
Non-public primary constructor is exposed via the generated 'copy()' method of the 'data' class.

The generated 'copy()' will change its visibility in future releases.

To suppress the warning do one of the following:
- Annotate the data class with the '@ConsistentCopyVisibility' annotation.
- Use the '-Xconsistent-data-class-copy-visibility' compiler flag.
- Annotate the data class with the '@ExposedCopyVisibility' annotation 
  (Discouraged, but can be used to keep binary compatibility).

To learn more, see the documentation of the '@ConsistentCopyVisibility' and '@ExposedCopyVisibility' annotations.



为了对这种行为有更多的控制,在Kotlin 2.0.20中,我们引入了两个注释:

  • @ConsistentCopyVisibility现在就可以选择加入该行为,否则我们会在以后的版本中将其设为默认值。
  • @ExposedCopyVisibility选择退出该行为,并在声明站点上抑制警告。注意,即使有了这个注释,当调用copy()函数时,编译器仍然会报告警告。

如果您想为整个模块而不是单个类选择2.0.20中已经有的新行为,您可以使用-Xconsistent-data-class-copy-visibility编译器选项。该选项与在模块中的所有数据类中添加@ConsistentCopyVisibility注释具有相同的效果。

三、标准库

标准库现在支持通用唯一标识符作为实验特性,并包括对Base64解码的一些更改。

3.1 在通用Kotlin标准库中支持uuid

本特性是实验性的。要选择加入,请使用@ExperimentalUuidApi注释或编译器选项-opt-in=kotlin.uuid.ExperimentalUuidApi。
Kotlin 2.0.20引入了一个类来表示通用Kotlin标准库中的uuid(通用唯一标识符),以解决唯一标识项的问题。

此外,该特性还为以下uuid相关操作提供了api:

  • 生成uuid。
  • 解析uid并将其格式化为字符串表示形式。
  • 从指定的128位值创建uuid。
  • 访问UUID的128位。

下面的代码示例演示了这些操作:

复制代码
@OptIn(ExperimentalUuidApi::class)
fun testUUIDs(){
    val byteArray = byteArrayOf(
        0x55, 0x0E, 0x84.toByte(), 0x00, 0xE2.toByte(), 0x9B.toByte(), 0x41, 0xD4.toByte(),
        0xA7.toByte(), 0x16, 0x44, 0x66, 0x55, 0x44, 0x00, 0x00
    )
    val uuid1 = Uuid.fromByteArray(byteArray)
    val uuid2 = Uuid.fromULongs(0x550E8400E29B41D4uL, 0xA716446655440000uL)
    val uuid3 = Uuid.parse("550e8400-e29b-41d4-a716-446655440000")
    println(uuid1)
// 550e8400-e29b-41d4-a716-446655440000
    println(uuid1 == uuid2)
// true
    println(uuid2 == uuid3)
// true

// Accesses UUID bits
    val version = uuid1.toLongs { mostSignificantBits, _ ->
        ((mostSignificantBits shr 12) and 0xF).toInt()
    }
    println(version)
// 4

// Generates a random UUID
    val randomUuid = Uuid.random()
    println(uuid1 == randomUuid)
// false
}

fun main() {
//    testConsistentCopyVisibility()
    testUUIDs()
}

运行结果

四、kt_2020.kt文件代码

复制代码
package com.example.test.ktversion

import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid

//https://kotlinlang.org/docs/whatsnew2020.html
// Triggers a warning in 2.0.20

// 数据类复制函数与构造函数具有相同的可见性
@ConsistentCopyVisibility
//data class PositiveInteger private constructor(val number: Int) {
//报错信息:Cannot access 'fun copy(number: Int = ...): PositiveInteger': it is private in
data class PositiveInteger constructor(val number: Int) {
    companion object {
        fun create(number: Int): PositiveInteger? = if (number > 0) PositiveInteger(number) else null
    }
}

/**
 *  数据类复制函数与构造函数具有相同的可见性
 *
 *  @ConsistentCopyVisibility 用于标记一个类或接口,以表明其副本在可见性方面应该是一致的。换句话说,当你创建一个类的副本时,副本的可见性应该与原始类实例的可见性相匹配。
 */
fun testConsistentCopyVisibility(){
    val positiveNumber = PositiveInteger.create(42) ?: return
    // Triggers a warning in 2.0.20
    val negativeNumber = positiveNumber.copy(number = -1)
    // Warning: Non-public primary constructor is exposed via the generated 'copy()' method of the 'data' class.
    // The generated 'copy()' will change its visibility in future releases.
}

// https://kotlinlang.org/docs/whatsnew2020.html#standard-library
// 通用的标准库支持 UUIDs
// Constructs a byte array for UUID creation
@OptIn(ExperimentalUuidApi::class)
fun testUUIDs(){
    val byteArray = byteArrayOf(
        0x55, 0x0E, 0x84.toByte(), 0x00, 0xE2.toByte(), 0x9B.toByte(), 0x41, 0xD4.toByte(),
        0xA7.toByte(), 0x16, 0x44, 0x66, 0x55, 0x44, 0x00, 0x00
    )
    val uuid1 = Uuid.fromByteArray(byteArray)
    val uuid2 = Uuid.fromULongs(0x550E8400E29B41D4uL, 0xA716446655440000uL)
    val uuid3 = Uuid.parse("550e8400-e29b-41d4-a716-446655440000")
    println(uuid1)
// 550e8400-e29b-41d4-a716-446655440000
    println(uuid1 == uuid2)
// true
    println(uuid2 == uuid3)
// true

// Accesses UUID bits
    val version = uuid1.toLongs { mostSignificantBits, _ ->
        ((mostSignificantBits shr 12) and 0xF).toInt()
    }
    println(version)
// 4

// Generates a random UUID
    val randomUuid = Uuid.random()
    println(uuid1 == randomUuid)
// false
}
fun main() {
    testConsistentCopyVisibility()
    testUUIDs()
}
相关推荐
云游云记3 分钟前
php 网络请求工具全解:cURL 与 Guzzle 总结
开发语言·网络·php
m0_7482299912 分钟前
帝国CMS后台搭建全攻略
java·c语言·开发语言·学习
TheNextByte113 分钟前
iPhone 与Android :有什么区别?
android·cocoa·iphone
weixin_4624462318 分钟前
PaddleX 3.2 人脸识别实战:自定义人脸库 + CartoonFace 官方案例 Top-K 识别完整指南
开发语言·r语言
_李小白28 分钟前
【Android 美颜相机】第二十一天:GPUImageChromaKeyBlendFilter (颜色加深混合滤镜)
android·数码相机
Testopia34 分钟前
走一遍 AI 学习之路 —— AI实例系列说明
开发语言·人工智能·python
Tony Bai37 分钟前
【分布式系统】11 理论的试金石:用 Go 从零实现一个迷你 Raft 共识
开发语言·后端·golang
Beginner x_u37 分钟前
JavaScript 原型、原型链与原型继承的核心机制解析
开发语言·javascript·原型模式·原型原型链
离离茶37 分钟前
【笔记1-11】Qt 关闭QToolbar的拓展菜单
开发语言·笔记·qt
曹牧39 分钟前
C#:窗体构造函数无法引用窗体控件
开发语言·c#