kotlin 2.0.0版本英文官方文档
The Kotlin 2.0.0 release is out and the new Kotlin K2 compiler is Stable! Additionally, here are some other highlights:
Kotlin 2.0.0发布了,新的Kotlin K2编译器已经稳定了。此外,以下是其他一些亮点:
New Compose compiler Gradle plugin
Generation of lambda functions using invokedynamic
The kotlinx-metadata-jvm library is now Stable
Monitoring GC performance in Kotlin/Native with signposts on Apple platforms
Resolving conflicts in Kotlin/Native with Objective-C methods
Support for named export in Kotlin/Wasm
Support for unsigned primitive types in functions with @JsExport in Kotlin/Wasm
Optimize production builds by default using Binaryen
New Gradle DSL for compiler options in multiplatform projects
Stable replacement of the enum class values generic function
// The compiler rejects previous smart cast information for
// stringInput. Now stringInput has the String? type.
stringInput = null
// Trigger an exception
if (2 > 1) throw Exception()
stringInput = ""
} catch (exception: Exception) {
// In Kotlin 2.0.0, the compiler knows stringInput
// can be null, so stringInput stays nullable.
println(stringInput?.length)
// null
println("捕获到的异常信息:$exception")
// In Kotlin 1.9.20, the compiler says that a safe call isn't
// needed, but this is incorrect.
}finally {
println("执行了 finally 逻辑")
}
}
复制代码
运行结果
八、kt_200.kt文件代码
package com.example.test.ktversion// https://kotlinlang.org/docs/whatsnew20.htmlclass Cat { //咕噜声 fun purr() { print("cat Purr purr") }}fun petAnimal(animal: Any) { val isCat = animal is Cat if (isCat) { //在Kotlin 2.0.0中,编译器可以访问关于isCat的信息, // 所以它知道动物被智能铸造为Cat范例。因此,可以调用purr()函数。 //在Kotlin 1.9.20中,编译器不知道 //调用purr()函数触发错误。 // animal.purr() // 2.0之前的版本, // (animal as Cat).purr() }}// Type checks with logical or operator 将对象的范例查抄与or操作符(||)结合起来interface Status {
fun signal() {}
}
interface Ok : Status
interface Postponed : Status
interface Declined : Status
fun signalCheck(signalStatus: Any) {
if (signalStatus is Postponed || signalStatus is Declined) {
// signalStatus is smart-cast to a common supertype Status; signalStatus被智能转换为一个普通的超类型Status
// signalStatus.signal()
// Prior to Kotlin 2.0.0, signalStatus is smart cast; 在Kotlin 2.0.0之前,signalStatus是智能强制转换
// to type Any, so calling the signal() function triggered an ; 输入Any,所以调用signal()函数会触发一个
// Unresolved reference error. The signal() function can only; 未解析的引用错误。signal()函数只能
// be called successfully after another type check: ; 在另一次类型检查后被成功调用:
// check(signalStatus is Status)
// signalStatus.signal()
}
}
// Inline functionsinterface Processor { fun process()}inline fun inlineAction(f: () -> Unit) = f()fun nextProcessor(): Processor? = nullfun runProcessor(): Processor? { var processor: Processor? = null inlineAction { // In Kotlin 2.0.0, the compiler knows that processor ; 在Kotlin 2.0.0中,编译器知道这个处理器 // is a local variable, and inlineAction() is an inline function, so ; 是一个局部变量,而inlineAction()是一个内联函数,所以 // references to processor can't be leaked. Therefore, it's safe ; 对处理器的引用不能泄露。因此,它是安全的 // to smart-cast processor. ; 到智能转换处理器。 // If processor isn't null, processor is smart-cast; 假如processor不为空,则processor是smart-cast if (processor != null) { // The compiler knows that processor isn't null, so no safe call; 编译器知道处理器不是null,所以没有安全调用 // is needed ; 需要// processor.process() // In Kotlin 1.9.20, you have to perform a safe call: // processor?.process() } processor = nextProcessor() } return processor}// Properties with function types 函数范例的属性class Holder(val provider: (() -> Unit)?) { fun process() { // In Kotlin 2.0.0, if provider isn't null, then // provider is smart-cast if (provider != null) { // The compiler knows that provider isn't null// provider() // In 1.9.20, the compiler doesn't know that provider isn't // null, so it triggers an error: // Reference has a nullable type '(() -> Unit)?', use explicit '?.invoke()' to make a function-like call instead } }}//This change also applies if you overload your invoke operator. For example:interface Provider { operator fun invoke()}interface Processor2 : () -> Stringclass Holder2(val provider: Provider?, val processor: Processor2?) { fun process() { if (provider != null) {// provider() // In 1.9.20, the compiler triggers an error: // Reference has a nullable type 'Provider?' use explicit '?.invoke()' to make a function-like call instead } }}// 非常处理fun testString() {
var stringInput: String? = null
// stringInput is smart-cast to String type
stringInput = ""
try {
// The compiler knows that stringInput isn't null
println(stringInput.length)
// 0
// The compiler rejects previous smart cast information for
// stringInput. Now stringInput has the String? type.
stringInput = null
// Trigger an exception
if (2 > 1) throw Exception()
stringInput = ""
} catch (exception: Exception) {
// In Kotlin 2.0.0, the compiler knows stringInput
// can be null, so stringInput stays nullable.
println(stringInput?.length)
// null
println("捕获到的异常信息:$exception")
// In Kotlin 1.9.20, the compiler says that a safe call isn't
// needed, but this is incorrect.
}finally {
println("执行了 finally 逻辑")
}
}
fun main() { val ketty = Cat() petAnimal(ketty) testString()}