Kotlin:1.9.0 的新特性

一、概述

Kotlin 1.9.0版本英语官方文档

Kotlin 1.9.0 中文官方文档

The Kotlin 1.9.0 release is out and the K2 compiler for the JVM is now in Beta. Additionally, here are some of the main highlights:

Kotlin 1.9.0版本已经发布,用于JVM的K2编译器现在处于Beta阶段。此外,以下是一些主要亮点:

主要演示的其中四个特性,测试结果图如下

二 、稳定版枚举类的 使用 entries 替代 values 函数

bash 复制代码
enum class Color(val colorName: String, var rgb: String) {
    RED("红色", "#FF0000"),
    ORANGE("橘色", "#FF7F00"),
    YELLOW("Yellow", "#FFFF00")
}

// https://book.kotlincn.net/text/whatsnew1820.html
// entries 属性是在 Kotlin 1.8.20 中作为实验性功能引入的,在 Kotlin 1.9.0 中已经稳定

// 稳定版枚举类的 使用 entries 替代 values 函数
fun findColoBy(rbg: String?): Color? = Color.entries.find { it.rgb == rbg }

enum class RGB { RED, GREEN, BLUE }

/**
 *  稳定版枚举类的 使用 entries 替代 values 函数
 */
fun testEnum() {
    //    for (color in RGB.values()) println(color.toString())
    for (color in RGB.entries) println(color.toString())
    //RED
    //GREEN
    //BLUE

    println("----------------------------------- end")
}

三、稳定版用于前闭后开区间的 ...< 操作符

bash 复制代码
fun testOpenIntervalWithFrontClosed() {

//    for (number in 2 until 6) {
//        println("number = $number")
//    }
    //前闭后开区间的 ..< 操作符
    for (number in 2..<6) {
        println("number = $number")
        //number = 2
        //number = 3
        //number = 4
        //number = 5
    }

    println("-------------------- 稳定版用于前闭后开区间的 ...< 操作符 ------- end")
}

四、新增按名称获取正则表达式捕获组的公共函数

bash 复制代码
/**
 * 新增按名称获取正则表达式捕获组的公共函数
 */

fun testRegex() {
    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"
    // 坐标:奥斯汀,德克萨斯州:123

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

    println("-------------------- 新增按名称获取正则表达式捕获组的公共函数 ------- end")
}

五、数据类相称的数据对象

bash 复制代码
//数据类相称的数据对象
sealed interface ReadResult
data class Number(val number: Int) : ReadResult
data class Text(val text: String) : ReadResult

data object EndOfFile : ReadResult

/**
 * 数据类相称的数据对象
 */
fun testDataObject(){
    println(Number(7))
    println(EndOfFile)
    //Number(number=7)
    //EndOfFile
    println("-------------------- 数据类相称的数据对象 ------ end")
}

六、kt_190文件完整代码

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

/*
    https://book.kotlincn.net/text/whatsnew19.html
 */

enum class Color(val colorName: String, var rgb: String) {
    RED("红色", "#FF0000"),
    ORANGE("橘色", "#FF7F00"),
    YELLOW("Yellow", "#FFFF00")
}

// https://book.kotlincn.net/text/whatsnew1820.html
// entries 属性是在 Kotlin 1.8.20 中作为实验性功能引入的,在 Kotlin 1.9.0 中已经稳定

// 稳定版枚举类的 使用 entries 替代 values 函数
fun findColoBy(rbg: String?): Color? = Color.entries.find { it.rgb == rbg }

enum class RGB { RED, GREEN, BLUE }


//数据类相称的数据对象
sealed interface ReadResult
data class Number(val number: Int) : ReadResult
data class Text(val text: String) : ReadResult

data object EndOfFile : ReadResult

/**
 *  稳定版枚举类的 使用 entries 替代 values 函数
 */
fun testEnum() {
    //    for (color in RGB.values()) println(color.toString())
    for (color in RGB.entries) println(color.toString())
    //RED
    //GREEN
    //BLUE

    println("-------------------  稳定版枚举类的 使用 entries 替代 values 函数 ----- end")
}

/**
 * 稳定版用于前闭后开区间的 ...< 操作符
 */
fun testOpenIntervalWithFrontClosed() {

//    for (number in 2 until 6) {
//        println("number = $number")
//    }
    //前闭后开区间的 ..< 操作符
    for (number in 2..<6) {
        println("number = $number")
        //number = 2
        //number = 3
        //number = 4
        //number = 5
    }

    println("-------------------- 稳定版用于前闭后开区间的 ...< 操作符 ------- end")
}

/**
 * 新增按名称获取正则表达式捕获组的公共函数
 */

fun testRegex() {
    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"
    // 坐标:奥斯汀,德克萨斯州:123

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

    println("-------------------- 新增按名称获取正则表达式捕获组的公共函数 ------- end")
}

/**
 * 数据类相称的数据对象
 */
fun testDataObject(){
    println(Number(7))
    println(EndOfFile)
    //Number(number=7)
    //EndOfFile
    println("-------------------- 数据类相称的数据对象 ------ end")
}

fun main() {
    testEnum()
    testOpenIntervalWithFrontClosed()
    testRegex()
    testDataObject()
}
相关推荐
艾莉丝努力练剑27 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
人生游戏牛马NPC1号2 小时前
学习 Flutter (三):玩安卓项目实战 - 上
android·学习·flutter
小馬佩德罗4 小时前
Android系统的问题分析笔记 - Android上的调试方式 debuggerd
android·调试
倔强青铜34 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
清霜之辰5 小时前
安卓基于 FirebaseAuth 实现 google 登录
android·google·auth·firebase
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
GitLqr5 小时前
数码洞察 | Apple VS DMA、三星新品、Android 16KB Page Size
android·ios·samsung
alexhilton5 小时前
SnapshotFlow还是collectAsState?对于Jetpack Compose来说哪个更香?
android·kotlin·android jetpack
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上5 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt