大家吼哇!就在三小時前,Kotlin v2.1.20 發布了,更新的內容也已經在官網上更新:What’s new in Kotlin 2.1.20 。
我粗略地看了一下,下面為大家選出一些我比較感興趣、且你可能也會感興趣的內容。
注意!這里只選了一些標準庫中的一些API之類的變化,不會包括諸如編譯器變動、工具(例如Gradle)變化等。
Atomic API
現在,在 Kotlin 的標準庫中可以使用原子類啦!在之前,想要使用原子類型,要么只能在 JVM 平臺中使用,要么需要自己手動實現,很麻煩,現在 Kotlin common API 中為標準庫添加了 kotlin.concurrent.atomics
包,其中包括了一些原子類型。
現在,你可以在任何Kotlin支持的平臺上使用原子類型了!
以下是官方示例:
@OptIn(ExperimentalAtomicApi::class)
suspend fun main() {// Initializes the atomic counter for processed itemsvar processedItems = AtomicInt(0)val totalItems = 100val items = List(totalItems) { "item$it" }// Splits the items into chunks for processing by multiple coroutinesval chunkSize = 20val itemChunks = items.chunked(chunkSize)coroutineScope {for (chunk in itemChunks) {launch {for (item in chunk) {println("Processing $item in thread ${Thread.currentThread()}")processedItems += 1 // Increment counter atomically}}}}
如果你熟悉Java中的原子API,那么對這些新增類型的使用肯定不會陌生。而提到 Java,這些原子類型也一如既往地提供了與 Java 類型互換的API asJavaAtomic
和 asKotlinAtomic
。
@OptIn(ExperimentalAtomicApi::class)
fun main() {// Converts Kotlin AtomicInt to Java's AtomicIntegerval kotlinAtomic = AtomicInt(42)val javaAtomic: AtomicInteger = kotlinAtomic.asJavaAtomic()println("Java atomic value: ${javaAtomic.get()}")// Java atomic value: 42// Converts Java's AtomicInteger back to Kotlin's AtomicIntval kotlinAgain: AtomicInt = javaAtomic.asKotlinAtomic()println("Kotlin atomic value: ${kotlinAgain.load()}")// Kotlin atomic value: 42
}
UUID
這個版本中,標準庫針對之前版本增加的 UUID
類型進行了一些增強和調整。
簡單來說:
- 改善了
parse()
的效果,現在不帶-
(破折號) 的 UUID 也能解析了。 - 增加了一些語義更明確的函數:
parseHexDash()
,toHexDashString()
。 - 為
UUID
實現Comparable
,也就是說現在UUID
是可以排序或者進行比較的了,比如使用sorted()
或使用操作符<
和>
等。
以下是官方示例:
@OptIn(ExperimentalUuidApi::class)
fun main() {// parse() accepts a UUID in a plain hexadecimal formatval uuid = Uuid.parse("550e8400e29b41d4a716446655440000")// Converts it to the hex-and-dash formatval hexDashFormat = uuid.toHexDashString()// Outputs the UUID in the hex-and-dash formatprintln(hexDashFormat)// Outputs UUIDs in ascending orderprintln(listOf(uuid,Uuid.parse("780e8400e29b41d4a716446655440005"),Uuid.parse("5ab88400e29b41d4a716446655440076")).sorted())}
新的時間跟蹤API
在 2.1.20 中,添加了一些原本只在 kotlinx-datetime
中才有的API。這下子在標準庫中操作時間終于要變得更簡單了,還不需要額外的依賴。
KMP庫狂喜!
- 引入了同
kotlinx.datetime.Clock
的接口kotlin.time.Clock
。 - 引入了同
kotlinx.datetime.Instant
的接口kotlin.time.Instant
。
當然,一如既往,它們也有與 Java 和 JS 進行相互轉化的API:
- JVM 平臺中
Instant
的toKotlinInstant()
和toJavaInstant()
。 - JS 平臺中使用
Instant.toJSDate()
可以將Instant
轉化為 JS 的Date
。
以下是官方示例:
import kotlin.time.*@OptIn(ExperimentalTime::class)
fun main() {// Get the current moment in timeval currentInstant = Clock.System.now()println("Current time: $currentInstant")// Find the difference between two moments in timeval pastInstant = Instant.parse("2023-01-01T00:00:00Z")val duration = currentInstant - pastInstantprintln("Time elapsed since 2023-01-01: $duration")
}
結尾
以上就是主要的標準庫變化啦!其他的變化內容也有不少,感興趣的話可以去官網了解喔,這些變化里我最喜歡的就是 atomic API 和 time API 進入標準庫了,為 KMP 庫作者省了不少事兒呢。
不說了,我又得跟進更新我的編譯器插件 kotlin-suspend-transform-compiler-plugin、繼續跟編譯器插件搏斗了,我們下次見,愛你?