Kotlin 1.5.30 的新特性

发布于:2021-08-24

Kotlin 1.5.30 offers language updates including previews of future changes, various improvements in platform support and tooling, and new standard library functions.

Here are some major improvements:

  • Language features, including experimental sealed when statements, changes in using opt-in requirement, and others
  • Native support for Apple silicon
  • Kotlin/JS IR backend reaches Beta
  • Improved Gradle plugin experience

You can also find a short overview of the changes in the release blog post and this video:

YouTube 视频:Kotlin 1.5.30

语言特性

Kotlin 1.5.30 is presenting previews of future language changes and bringing improvements to the opt-in requirement mechanism and type inference:

对于密封类与布尔值主语穷尽 when 语句

Support for sealed (exhaustive) when statements is Experimental. It may be dropped or changed at any time. Opt-in is required (see the details below), and you should use it only for evaluation purposes. We would appreciate your feedback on it in YouTrack.

Kotlin 1.5.30 - 图1

An exhaustive when statement contains branches for all possible types or values of its subject or for some types plus an else branch. In other words, it covers all possible cases.

We’re planning to prohibit non-exhaustive when statements soon to make the behavior consistent with when expressions. To ensure smooth migration, you can configure the compiler to report warnings about non-exhaustive when statements with a sealed class or a Boolean. Such warnings will appear by default in Kotlin 1.6 and will become errors later.

Enums already get a warning.

Kotlin 1.5.30 - 图2

  1. sealed class Mode {
  2. object ON : Mode()
  3. object OFF : Mode()
  4. }
  5. fun main() {
  6. val x: Mode = Mode.ON
  7. when (x) {
  8. Mode.ON -> println("ON")
  9. }
  10. // WARNING: Non exhaustive 'when' statements on sealed classes/interfaces
  11. // will be prohibited in 1.7, add an 'OFF' or 'else' branch instead
  12. val y: Boolean = true
  13. when (y) {
  14. true -> println("true")
  15. }
  16. // WARNING: Non exhaustive 'when' statements on Booleans will be prohibited
  17. // in 1.7, add a 'false' or 'else' branch instead
  18. }

To enable this feature in Kotlin 1.5.30, use language version 1.6. You can also change the warnings to errors by enabling progressive mode.

【Kotlin】

  1. kotlin {
  2. sourceSets.all {
  3. languageSettings.apply {
  4. languageVersion = "1.6"
  5. //progressiveMode = true // false by default
  6. }
  7. }
  8. }

【Groovy】

  1. kotlin {
  2. sourceSets.all {
  3. languageSettings {
  4. languageVersion = '1.6'
  5. //progressiveMode = true // false by default
  6. }
  7. }
  8. }

挂起函数作为超类型

Support for suspending functions as supertypes is Experimental. It may be dropped or changed at any time. Opt-in is required (see the details below), and you should use it only for evaluation purposes. We would appreciate your feedback on it in YouTrack.

Kotlin 1.5.30 - 图3

Kotlin 1.5.30 provides a preview of the ability to use a suspend functional type as a supertype with some limitations.

  1. class MyClass: suspend () -> Unit {
  2. override suspend fun invoke() { TODO() }
  3. }

Use the -language-version 1.6 compiler option to enable the feature:

【Kotlin】

  1. kotlin {
  2. sourceSets.all {
  3. languageSettings.apply {
  4. languageVersion = "1.6"
  5. }
  6. }
  7. }

【Groovy】

  1. kotlin {
  2. sourceSets.all {
  3. languageSettings {
  4. languageVersion = '1.6'
  5. }
  6. }
  7. }

The feature has the following restrictions:

  • You can’t mix an ordinary functional type and a suspend functional type as supertype. This is because of the implementation details of suspend functional types in the JVM backend. They are represented in it as ordinary functional types with a marker interface. Because of the marker interface, there is no way to tell which of the superinterfaces are suspended and which are ordinary.
  • You can’t use multiple suspend functional supertypes. If there are type checks, you also can’t use multiple ordinary functional supertypes.

对隐式用到实验性 API 要求选择加入

The opt-in requirement mechanism is Experimental. It may change at any time. See how to opt-in. Use it only for evaluation purposes. We would appreciate your feedback on it in YouTrack.

Kotlin 1.5.30 - 图4

The author of a library can mark an experimental API as requiring opt-in to inform users about its experimental state. The compiler raises a warning or error when the API is used and requires explicit consent to suppress it.

In Kotlin 1.5.30, the compiler treats any declaration that has an experimental type in the signature as experimental. Namely, it requires opt-in even for implicit usages of an experimental API. For example, if the function’s return type is marked as an experimental API element, a usage of the function requires you to opt-in even if the declaration is not marked as requiring an opt-in explicitly.

  1. // Library code
  2. @RequiresOptIn(message = "This API is experimental.")
  3. @Retention(AnnotationRetention.BINARY)
  4. @Target(AnnotationTarget.CLASS)
  5. annotation class MyDateTime // Opt-in requirement annotation
  6. @MyDateTime
  7. class DateProvider // A class requiring opt-in
  8. // Client code
  9. // Warning: experimental API usage
  10. fun createDateSource(): DateProvider { /* ... */ }
  11. fun getDate(): Date {
  12. val dateSource = createDateSource() // Also warning: experimental API usage
  13. // ...
  14. }

Learn more about opt-in requirements.

对使用选择加入要求的注解不同目标的变更

The opt-in requirement mechanism is Experimental. It may change at any time. See how to opt-in. Use it only for evaluation purposes. We would appreciate your feedback on it in YouTrack.

Kotlin 1.5.30 - 图5

Kotlin 1.5.30 presents new rules for using and declaring opt-in requirement annotations on different targets. The compiler now reports an error for use cases that are impractical to handle at compile time. In Kotlin 1.5.30:

  • Marking local variables and value parameters with opt-in requirement annotations is forbidden at the use site.
  • Marking override is allowed only if its basic declaration is also marked.
  • Marking backing fields and getters is forbidden. You can mark the basic property instead.
  • Setting TYPE and TYPE_PARAMETER annotation targets is forbidden at the opt-in requirement annotation declaration site.

Learn more about opt-in requirements.

递归泛型类型的类型推断改进

In Kotlin and Java, you can define a recursive generic type, which references itself in its type parameters. In Kotlin 1.5.30, the Kotlin compiler can infer a type argument based only on upper bounds of the corresponding type parameter if it is a recursive generic. This makes it possible to create various patterns with recursive generic types that are often used in Java to make builder APIs.

  1. // Kotlin 1.5.20
  2. val containerA = PostgreSQLContainer<Nothing>(DockerImageName.parse("postgres:13-alpine")).apply {
  3. withDatabaseName("db")
  4. withUsername("user")
  5. withPassword("password")
  6. withInitScript("sql/schema.sql")
  7. }
  8. // Kotlin 1.5.30
  9. val containerB = PostgreSQLContainer(DockerImageName.parse("postgres:13-alpine"))
  10. .withDatabaseName("db")
  11. .withUsername("user")
  12. .withPassword("password")
  13. .withInitScript("sql/schema.sql")

You can enable the improvements by passing the -Xself-upper-bound-inference or the -language-version 1.6 compiler options. See other examples of newly supported use cases in this YouTrack ticket.

消除构建器推断限制

Builder inference is a special kind of type inference that allows you to infer the type arguments of a call based on type information from other calls inside its lambda argument. This can be useful when calling generic builder functions such as buildList() or sequence(): buildList { add("string") }.

Inside such a lambda argument, there was previously a limitation on using the type information that the builder inference tries to infer. This means you can only specify it and cannot get it. For example, you cannot call get() inside a lambda argument of buildList() without explicitly specified type arguments.

Kotlin 1.5.30 removes these limitations with the -Xunrestricted-builder-inference compiler option. Add this option to enable previously prohibited calls inside a lambda argument of generic builder functions:

  1. @kotlin.ExperimentalStdlibApi
  2. val list = buildList {
  3. add("a")
  4. add("b")
  5. set(1, null)
  6. val x = get(1)
  7. if (x != null) {
  8. removeAt(1)
  9. }
  10. }
  11. @kotlin.ExperimentalStdlibApi
  12. val map = buildMap {
  13. put("a", 1)
  14. put("b", 1.1)
  15. put("c", 2f)
  16. }

Also, you can enable this feature with the -language-version 1.6 compiler option.

Kotlin/JVM

With Kotlin 1.5.30, Kotlin/JVM receives the following features:

See the Gradle section for Kotlin Gradle plugin updates on the JVM platform.

注解类的实例化

注解类的实例化 is Experimental. It may be dropped or changed at any time. Opt-in is required (see the details below), and you should use it only for evaluation purposes. We would appreciate your feedback on it in YouTrack.

Kotlin 1.5.30 - 图6

With Kotlin 1.5.30 you can now call constructors of annotation classes in arbitrary code to obtain a resulting instance. This feature covers the same use cases as the Java convention that allows the implementation of an annotation interface.

  1. annotation class InfoMarker(val info: String)
  2. fun processInfo(marker: InfoMarker) = ...
  3. fun main(args: Array<String>) {
  4. if (args.size != 0)
  5. processInfo(getAnnotationReflective(args))
  6. else
  7. processInfo(InfoMarker("default"))
  8. }

Use the -language-version 1.6 compiler option to enable this feature. Note that all current annotation class limitations, such as restrictions to define non-val parameters or members different from secondary constructors, remain intact.

Learn more about instantiation of annotation classes in this KEEP

改进了对可空性注解配置的支持

The Kotlin compiler can read various types of nullability annotations to get nullability information from Java. This information allows it to report nullability mismatches in Kotlin when calling Java code.

In Kotlin 1.5.30, you can specify whether the compiler reports a nullability mismatch based on the information from specific types of nullability annotations. Just use the compiler option -Xnullability-annotations=@<package-name>:<report-level>. In the argument, specify the fully qualified nullability annotations package and one of these report levels:

  • ignore to ignore nullability mismatches
  • warn to report warnings
  • strict to report errors.

See the full list of supported nullability annotations along with their fully qualified package names.

Here is an example showing how to enable error reporting for the newly supported RxJava 3 nullability annotations: [[email protected]](https://book.kotlincn.net/cdn-cgi/l/email-protection):strict. Note that all such nullability mismatches are warnings by default.

Kotlin/Native

Kotlin/Native has received various changes and improvements:

Apple silicon 支持

Kotlin 1.5.30 introduces native support for Apple silicon.

Previously, the Kotlin/Native compiler and tooling required the Rosetta translation environment for working on Apple silicon hosts. In Kotlin 1.5.30, the translation environment is no longer needed – the compiler and tooling can run on Apple silicon hardware without requiring any additional actions.

We’ve also introduced new targets that make Kotlin code run natively on Apple silicon:

  • macosArm64
  • iosSimulatorArm64
  • watchosSimulatorArm64
  • tvosSimulatorArm64

They are available on both Intel-based and Apple silicon hosts. All existing targets are available on Apple silicon hosts as well.

Note that in 1.5.30 we provide only basic support for Apple silicon targets in the kotlin-multiplatform Gradle plugin. Particularly, the new simulator targets aren’t included in the ios, tvos, and watchos target shortcuts. Learn how to use Apple silicon targets with the target shortcuts. We will keep working to improve the user experience with the new targets.

改进了用于 CocoaPods Gradle 插件的 Kotlin DSL

用于 Kotlin/Native frameworks 的新的参数

Kotlin 1.5.30 introduces the improved CocoaPods Gradle plugin DSL for Kotlin/Native frameworks. In addition to the name of the framework, you can specify other parameters in the Pod configuration:

  • Specify the dynamic or static version of the framework
  • Enable export dependencies explicitly
  • Enable Bitcode embedding

To use the new DSL, update your project to Kotlin 1.5.30, and specify the parameters in the cocoapods section of your build.gradle(.kts) file:

  1. cocoapods {
  2. frameworkName = "MyFramework" // This property is deprecated
  3. // and will be removed in future versions
  4. // New DSL for framework configuration:
  5. framework {
  6. // All Framework properties are supported
  7. // Framework name configuration. Use this property instead of
  8. // deprecated 'frameworkName'
  9. baseName = "MyFramework"
  10. // Dynamic framework support
  11. isStatic = false
  12. // Dependency export
  13. export(project(":anotherKMMModule"))
  14. transitiveExport = false // This is default.
  15. // Bitcode embedding
  16. embedBitcode(BITCODE)
  17. }
  18. }

支持 Xcode 配置的自定义名称

The Kotlin CocoaPods Gradle plugin supports custom names in the Xcode build configuration. It will also help you if you’re using special names for the build configuration in Xcode, for example Staging.

To specify a custom name, use the xcodeConfigurationToNativeBuildType parameter in the cocoapods section of your build.gradle(.kts) file:

  1. cocoapods {
  2. // Maps custom Xcode configuration to NativeBuildType
  3. xcodeConfigurationToNativeBuildType["CUSTOM_DEBUG"] = NativeBuildType.DEBUG
  4. xcodeConfigurationToNativeBuildType["CUSTOM_RELEASE"] = NativeBuildType.RELEASE
  5. }

This parameter will not appear in the Podspec file. When Xcode runs the Gradle build process, the Kotlin CocoaPods Gradle plugin will select the necessary native build type.

There’s no need to declare the Debug and Release configurations because they are supported by default.

Kotlin 1.5.30 - 图7

与 Swift 5.5 async/await 的实验性互操作

Concurrency interoperability with Swift async/await is Experimental. It may be dropped or changed at any time. You should use it only for evaluation purposes. We would appreciate your feedback on it in YouTrack.

Kotlin 1.5.30 - 图8

We added support for calling Kotlin’s suspending functions from Objective-C and Swift in 1.4.0, and now we’re improving it to keep up with a new Swift 5.5 feature – concurrency with async and await modifiers.

The Kotlin/Native compiler now emits the _Nullable_result attribute in the generated Objective-C headers for suspending functions with nullable return types. This makes it possible to call them from Swift as async functions with the proper nullability.

Note that this feature is experimental and can be affected in the future by changes in both Kotlin and Swift. For now, we’re offering a preview of this feature that has certain limitations, and we are eager to hear what you think. Learn more about its current state and leave your feedback in this YouTrack issue.

改进了对象与伴生对象的 Swift/Objective-C 映射

Getting objects and companion objects can now be done in a way that is more intuitive for native iOS developers. For example, if you have the following objects in Kotlin:

  1. object MyObject {
  2. val x = "Some value"
  3. }
  4. class MyClass {
  5. companion object {
  6. val x = "Some value"
  7. }
  8. }

To access them in Swift, you can use the shared and companion properties:

  1. MyObject.shared
  2. MyObject.shared.x
  3. MyClass.companion
  4. MyClass.Companion.shared

Learn more about Swift/Objective-C interoperability.

对于 MinGW 目标弃用了链接到 DLL 而未导入库的用法

LLD is a linker from the LLVM project, which we plan to start using in Kotlin/Native for MinGW targets because of its benefits over the default ld.bfd – primarily its better performance.

However, the latest stable version of LLD doesn’t support direct linkage against DLL for MinGW (Windows) targets. Such linkage requires using import libraries. Although they aren’t needed with Kotlin/Native 1.5.30, we’re adding a warning to inform you that such usage is incompatible with LLD that will become the default linker for MinGW in the future.

Please share your thoughts and concerns about the transition to the LLD linker in this YouTrack issue.

Kotlin 多平台

1.5.30 brings the following notable updates to Kotlin Multiplatform:

能在共享的原生代码中使用自定义 cinterop

Kotlin Multiplatform gives you an option to use platform-dependent interop libraries in shared source sets. Before 1.5.30, this worked only with platform libraries shipped with Kotlin/Native distribution. Starting from 1.5.30, you can use it with your custom cinterop libraries. To enable this feature, add the kotlin.mpp.enableCInteropCommonization=true property in your gradle.properties:

  1. kotlin.mpp.enableGranularSourceSetsMetadata=true
  2. kotlin.native.enableDependencyPropagation=false
  3. kotlin.mpp.enableCInteropCommonization=true

对 XCFrameworks 的支持

All Kotlin Multiplatform projects can now have XCFrameworks as an output format. Apple introduced XCFrameworks as a replacement for universal (fat) frameworks. With the help of XCFrameworks you:

  • Can gather logic for all the target platforms and architectures in a single bundle.
  • Don’t need to remove all unnecessary architectures before publishing the application to the App Store.

XCFrameworks is useful if you want to use your Kotlin framework for devices and simulators on Apple M1.

To use XCFrameworks, update your build.gradle(.kts) script:

【Kotlin】

  1. import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework
  2. plugins {
  3. kotlin("multiplatform")
  4. }
  5. kotlin {
  6. val xcf = XCFramework()
  7. ios {
  8. binaries.framework {
  9. baseName = "shared"
  10. xcf.add(this)
  11. }
  12. }
  13. watchos {
  14. binaries.framework {
  15. baseName = "shared"
  16. xcf.add(this)
  17. }
  18. }
  19. tvos {
  20. binaries.framework {
  21. baseName = "shared"
  22. xcf.add(this)
  23. }
  24. }
  25. }

【Groovy】

  1. import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFrameworkConfig
  2. plugins {
  3. id 'org.jetbrains.kotlin.multiplatform'
  4. }
  5. kotlin {
  6. def xcf = new XCFrameworkConfig(project)
  7. ios {
  8. binaries.framework {
  9. baseName = "shared"
  10. xcf.add(it)
  11. }
  12. }
  13. watchos {
  14. binaries.framework {
  15. baseName = "shared"
  16. xcf.add(it)
  17. }
  18. }
  19. tvos {
  20. binaries.framework {
  21. baseName = "shared"
  22. xcf.add(it)
  23. }
  24. }
  25. }

When you declare XCFrameworks, these new Gradle tasks will be registered:

  • assembleXCFramework
  • assembleDebugXCFramework (additionally debug artifact that contains dSYMs)
  • assembleReleaseXCFramework

Learn more about XCFrameworks in this WWDC video.

Android 构件的新版默认发布设置

Using the maven-publish Gradle plugin, you can publish your multiplatform library for the Android target by specifying Android variant names in the build script. The Kotlin Gradle plugin will generate publications automatically.

Before 1.5.30, the generated publication metadata included the build type attributes for every published Android variant, making it compatible only with the same build type used by the library consumer. Kotlin 1.5.30 introduces a new default publishing setup:

  • If all Android variants that the project publishes have the same build type attribute, then the published variants won’t have the build type attribute and will be compatible with any build type.
  • If the published variants have different build type attributes, then only those with the release value will be published without the build type attribute. This makes the release variants compatible with any build type on the consumer side, while non-release variants will only be compatible with the matching consumer build types.

To opt-out and keep the build type attributes for all variants, you can set this Gradle property: kotlin.android.buildTypeAttribute.keep=true.

Kotlin/JS

Two major improvements are coming to Kotlin/JS with 1.5.30:

JS IR 编译器后端达到 Beta 版

The IR-based compiler backend for Kotlin/JS, which was introduced in 1.4.0 in Alpha, has reached Beta.

Previously, we published the migration guide for the JS IR backend to help you migrate your projects to the new backend. Now we would like to present the Kotlin/JS Inspection Pack IDE plugin, which displays the required changes directly in IntelliJ IDEA.

为使用 Kotlin/JS IR 后端的应用程序提供更好的调试体验

Kotlin 1.5.30 brings JavaScript source map generation for the Kotlin/JS IR backend. This will improve the Kotlin/JS debugging experience when the IR backend is enabled, with full debugging support that includes breakpoints, stepping, and readable stack traces with proper source references.

Learn how to debug Kotlin/JS in the browser or IntelliJ IDEA Ultimate.

Gradle

As a part of our mission to improve the Kotlin Gradle plugin user experience, we’ve implemented the following features:

支持 Java toolchains

Gradle 6.7 introduced the “Java toolchains support” feature. Using this feature, you can:

  • Run compilations, tests, and executables using JDKs and JREs that are different from the Gradle ones.
  • Compile and test code with an unreleased language version.

With toolchains support, Gradle can autodetect local JDKs and install missing JDKs that Gradle requires for the build. Now Gradle itself can run on any JDK and still reuse the build cache feature.

The Kotlin Gradle plugin supports Java toolchains for Kotlin/JVM compilation tasks. A Java toolchain:

Use the following code to set a toolchain. Replace the placeholder <MAJOR_JDK_VERSION> with the JDK version you would like to use:

【Kotlin】

  1. kotlin {
  2. jvmToolchain {
  3. (this as JavaToolchainSpec).languageVersion.set(JavaLanguageVersion.of(<MAJOR_JDK_VERSION>)) // "8"
  4. }
  5. }

【Groovy】

  1. kotlin {
  2. jvmToolchain {
  3. languageVersion.set(JavaLanguageVersion.of(<MAJOR_JDK_VERSION>)) // "8"
  4. }
  5. }

Note that setting a toolchain via the kotlin extension will update the toolchain for Java compile tasks as well.

You can set a toolchain via the java extension, and Kotlin compilation tasks will use it:

  1. java {
  2. toolchain {
  3. languageVersion.set(JavaLanguageVersion.of(<MAJOR_JDK_VERSION>)) // "8"
  4. }
  5. }

For information about setting any JDK version for KotlinCompile tasks, look through the docs about setting the JDK version with the Task DSL.

For Gradle versions from 6.1 to 6.6, use the UsesKotlinJavaToolchain interface to set the JDK home.

能够使用 UsesKotlinJavaToolchain 接口指定 JDK home

All Kotlin tasks that support setting the JDK via kotlinOptions now implement the UsesKotlinJavaToolchain interface. To set the JDK home, put a path to your JDK and replace the <JDK_VERSION> placeholder:

【Kotlin】

  1. project.tasks
  2. .withType<UsesKotlinJavaToolchain>()
  3. .configureEach {
  4. it.kotlinJavaToolchain.jdk.use(
  5. "/path/to/local/jdk",
  6. JavaVersion.<LOCAL_JDK_VERSION>
  7. )
  8. }

【Groovy】

  1. project.tasks
  2. .withType(UsesKotlinJavaToolchain.class)
  3. .configureEach {
  4. it.kotlinJavaToolchain.jdk.use(
  5. '/path/to/local/jdk',
  6. JavaVersion.<LOCAL_JDK_VERSION>
  7. )
  8. }

Use the UsesKotlinJavaToolchain interface for Gradle versions from 6.1 to 6.6. Starting from Gradle 6.7, use the Java toolchains instead.

When using this feature, note that kapt task workers will only use process isolation mode, and the kapt.workers.isolation property will be ignored.

显式指定 Kotlin 守护进程 JVM 参数的更简单方式

In Kotlin 1.5.30, there’s a new logic for the Kotlin daemon’s JVM arguments. Each of the options in the following list overrides the ones that came before it:

  • If nothing is specified, the Kotlin daemon inherits arguments from the Gradle daemon (as before). For example, in the gradle.properties file:

    1. org.gradle.jvmargs=-Xmx1500m -Xms=500m
  • If the Gradle daemon’s JVM arguments have the kotlin.daemon.jvm.options system property, use it as before:

    1. org.gradle.jvmargs=-Dkotlin.daemon.jvm.options=-Xmx1500m -Xms=500m
  • You can add thekotlin.daemon.jvmargs property in the gradle.properties file:

    1. kotlin.daemon.jvmargs=-Xmx1500m -Xms=500m
  • You can specify arguments in the kotlin extension:

【Kotlin】

  1. kotlin {
  2. kotlinDaemonJvmArgs = listOf("-Xmx486m", "-Xms256m", "-XX:+UseParallelGC")
  3. }

【Groovy】

  1. kotlin {
  2. kotlinDaemonJvmArgs = ["-Xmx486m", "-Xms256m", "-XX:+UseParallelGC"]
  3. }
  • You can specify arguments for a specific task:

【Kotlin】

  1. tasks
  2. .matching { it.name == "compileKotlin" && it is CompileUsingKotlinDaemon }
  3. .configureEach {
  4. (this as CompileUsingKotlinDaemon).kotlinDaemonJvmArguments.set(listOf("-Xmx486m", "-Xms256m", "-XX:+UseParallelGC"))
  5. }

【Groovy】

  1. tasks
  2. .matching {
  3. it.name == "compileKotlin" && it instanceof CompileUsingKotlinDaemon
  4. }
  5. .configureEach {
  6. kotlinDaemonJvmArguments.set(["-Xmx1g", "-Xms512m"])
  7. }
  1. > In this case a new Kotlin daemon instance can start on task execution. Learn more about [the Kotlin daemon's interactions with JVM arguments](gradle-compilation-and-caches.md#setting-kotlin-daemons-jvm-arguments).
  2. >
  3. <svg width="24" height="24" fill="#4dbb5f" viewBox="0 0 24 24"><path d="M21 12a9 9 0 1 1-9-9 9 9 0 0 1 9 9zM10.5 7.5A1.5 1.5 0 1 0 12 6a1.5 1.5 0 0 0-1.5 1.5zm-.5 3.54v1h1V18h2v-6a.96.96 0 0 0-.96-.96z"></path></svg>

For more information about the Kotlin daemon, see the Kotlin daemon and using it with Gradle.

标准库

Kotlin 1.5.30 is bringing improvements to the standard library’s Duration and Regex APIs:

变更 Duration.toString() 输出

The Duration API is Experimental. It may be dropped or changed at any time. Use it only for evaluation purposes. We would appreciate hearing your feedback on it in YouTrack.

Kotlin 1.5.30 - 图10

Before Kotlin 1.5.30, the Duration.toString() function would return a string representation of its argument expressed in the unit that yielded the most compact and readable number value. From now on, it will return a string value expressed as a combination of numeric components, each in its own unit. Each component is a number followed by the unit’s abbreviated name: d, h, m, s. For example:

Example of function callPrevious outputCurrent output
Duration.days(45).toString()45.0d45d
Duration.days(1.5).toString()36.0h1d 12h
Duration.minutes(1230).toString()20.5h20h 30m
Duration.minutes(2415).toString()40.3h1d 16h 15m
Duration.minutes(920).toString()920m15h 20m
Duration.seconds(1.546).toString()1.55s1.546s
Duration.milliseconds(25.12).toString()25.1ms25.12ms

The way negative durations are represented has also been changed. A negative duration is prefixed with a minus sign (-), and if it consists of multiple components, it is surrounded with parentheses: -12m and -(1h 30m).

Note that small durations of less than one second are represented as a single number with one of the subsecond units. For example, ms (milliseconds), us (microseconds), or ns (nanoseconds): 140.884ms, 500us, 24ns. Scientific notation is no longer used to represent them.

If you want to express duration in a single unit, use the overloaded Duration.toString(unit, decimals) function.

We recommend using Duration.toIsoString() in certain cases, including serialization and interchange. Duration.toIsoString() uses the stricter ISO-8601 format instead of Duration.toString().

Kotlin 1.5.30 - 图11

由 String 解析 Duration

The Duration API is Experimental. It may be dropped or changed at any time. Use it only for evaluation purposes. We would appreciate hearing your feedback on it in this issue.

Kotlin 1.5.30 - 图12

In Kotlin 1.5.30, there are new functions in the Duration API:

Here are some examples of parse() and parseOrNull() usages:

  1. import kotlin.time.Duration
  2. import kotlin.time.ExperimentalTime
  3. @ExperimentalTime
  4. fun main() {
  5. //sampleStart
  6. val isoFormatString = "PT1H30M"
  7. val defaultFormatString = "1h 30m"
  8. val singleUnitFormatString = "1.5h"
  9. val invalidFormatString = "1 hour 30 minutes"
  10. println(Duration.parse(isoFormatString)) // "1h 30m"
  11. println(Duration.parse(defaultFormatString)) // "1h 30m"
  12. println(Duration.parse(singleUnitFormatString)) // "1h 30m"
  13. //println(Duration.parse(invalidFormatString)) // throws exception
  14. println(Duration.parseOrNull(invalidFormatString)) // "null"
  15. //sampleEnd
  16. }

And here are some examples of parseIsoString() and parseIsoStringOrNull() usages:

  1. import kotlin.time.Duration
  2. import kotlin.time.ExperimentalTime
  3. @ExperimentalTime
  4. fun main() {
  5. //sampleStart
  6. val isoFormatString = "PT1H30M"
  7. val defaultFormatString = "1h 30m"
  8. println(Duration.parseIsoString(isoFormatString)) // "1h 30m"
  9. //println(Duration.parseIsoString(defaultFormatString)) // throws exception
  10. println(Duration.parseIsoStringOrNull(defaultFormatString)) // "null"
  11. //sampleEnd
  12. }

在特定位置匹配 Regex

Regex.matchAt() and Regex.matchesAt() functions are Experimental. They may be dropped or changed at any time. Use them only for evaluation purposes. We would appreciate hearing your feedback on them in YouTrack.

Kotlin 1.5.30 - 图13

The new Regex.matchAt() and Regex.matchesAt() functions provide a way to check whether a regex has an exact match at a particular position in a String or CharSequence.

matchesAt() returns a boolean result:

  1. fun main(){
  2. //sampleStart
  3. val releaseText = "Kotlin 1.5.30 is released!"
  4. // regular expression: one digit, dot, one digit, dot, one or more digits
  5. val versionRegex = "\\d[.]\\d[.]\\d+".toRegex()
  6. println(versionRegex.matchesAt(releaseText, 0)) // "false"
  7. println(versionRegex.matchesAt(releaseText, 7)) // "true"
  8. //sampleEnd
  9. }

matchAt() returns the match if one is found or null if one isn’t:

  1. fun main(){
  2. //sampleStart
  3. val releaseText = "Kotlin 1.5.30 is released!"
  4. val versionRegex = "\\d[.]\\d[.]\\d+".toRegex()
  5. println(versionRegex.matchAt(releaseText, 0)) // "null"
  6. println(versionRegex.matchAt(releaseText, 7)?.value) // "1.5.30"
  7. //sampleEnd
  8. }

按 Regex 拆分为序列

Regex.splitToSequence() and CharSequence.splitToSequence(Regex) functions are Experimental. They may be dropped or changed at any time. Use them only for evaluation purposes. We would appreciate hearing your feedback on them in YouTrack.

Kotlin 1.5.30 - 图14

The new Regex.splitToSequence() function is a lazy counterpart of split(). It splits the string around matches of the given regex, but it returns the result as a Sequence so that all operations on this result are executed lazily.

  1. fun main(){
  2. //sampleStart
  3. val colorsText = "green, red , brown&blue, orange, pink&green"
  4. val regex = "[,\\s]+".toRegex()
  5. val mixedColor = regex.splitToSequence(colorsText)
  6. .onEach { println(it) }
  7. .firstOrNull { it.contains('&') }
  8. println(mixedColor) // "brown&blue"
  9. //sampleEnd
  10. }

A similar function was also added to CharSequence:

  1. val mixedColor = colorsText.splitToSequence(regex)

serialization 1.3.0-RC

kotlinx.serialization 1.3.0-RC is here with new JSON serialization capabilities:

  • Java IO streams serialization
  • Property-level control over default values
  • An option to exclude null values from serialization
  • Custom class discriminators in polymorphic serialization

Learn more in the changelog.