BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Kotlin 1.6.0 Released

Kotlin 1.6.0 Released

This item in japanese

Bookmarks

JetBrains has released Kotlin 1.6.0 which ships with language and standard library features from Kotlin 1.5.30 which are now Stable. One of the new language features is sealed when statements:

sealed class Employee {
    data class Developer(val languages: List<String>) : Employee()
    data class Operations(val operatingSystems: List<String>) : Employee()
}

fun printInformation(employee: Employee) {
    when (employee) {
        is Employee.Developer -> printLanguages(employee.languages)
    }
}

Kotlin already provides verified when expressions for sealed classes, enums, and Boolean types for exhaustiveness. Using a non-exhaustive when statement will result in an error in Kotlin 1.7, but for now, Kotlin 1.6.0 will display the following warning:

Non exhaustive 'when' statements on sealed class/interface will be 
prohibited in 1.7, add 'is Operations' branch or 'else' branch instead

Coroutines now support implementing suspend functional types as supertypes. Previously, it was only possible to use lambdas and suspending function references for the desired behavior. Now, a separate class may be used to implement the suspending functional type:

class OrderManager : suspend () -> Unit {
    override suspend fun invoke() { ... }
}

fun buy(action: suspend () -> Unit) {}

Conversions from regular to suspending functional types are now implicitly converted for any form of expression after previously being limited to functional literals and callable references. This makes it possible to use a regular function as a parameter for a suspending type:

val regularFunction = ::function
flow.collect(regularFunction)

Type inference for recursive generic types is now possible based on the upper bounds of the type parameter.

Changes in the standard library include readln() as an alternative for readLine()!! which throws an exception at the end of the file. Similarly, readlnOrNull() may be used as an alternative for readLine() which returns null when the end of the file is reached.

The typeOf() function was already available as an experimental API for the JVM platform and is now officially available for all platforms.

Collection builders like buildList(), buildMap() and buildSet() are now stable and return serializable collections in their read-only state.

The Duration API toComponents() function now accepts a Long instead of an Int for the number of days to avoid problems with larger numbers. The DurationUnit enum is no longer a type alias for java.util.concurrent.TimeUnit on the JVM as it wasn’t deemed necessary. Extension properties like Int.seconds are available through the Companion of the Duration class to limit their scope.

Other now stable functions include rotateLeft() and rotateRight() for integer bit rotations, splitToSequence() which splits a string based on a regular expression, Comparable.compareTo() may now be used in infix notation and replace() and replaceFirst() now give the same results on the JVM and JS for group references.

With Kotlin 1.6.0, it’s possible to develop based on the three previous API versions and the current stable one which currently includes 1.3, 1.4, 1.5 and 1.6.

Kotlin’s @kotlin.annotation.Repeatable is now compatible with Java and accepts the retention and may be used repeatedly. Java’s repeatable annotation also works in Kotlin.

A new experimental version, which should only be used for evaluation, of the memory manager is provided to remove the technical differences between native and JVM platforms. This enables leak-free concurrent programming primitive without the need for annotations or other configuration from the developers. Kotlin/Native now also supports Xcode 13 and allows compilation of Windows targets on all hosts that support Kotlin/Native.

Downloads of Node.js and Yarn for Kotlin/JS projects can be disabled when they’re already installed.

The Kover Gradle plugin is now available to measure code coverage for Kotlin code built with the Kotlin JVM compiler. Before the release of the Kover plugin, measurement of code coverage was a challenge as, for instance, JaCoCo wasn’t integrated with the Gradle toolchain and multiplatform projects.

The Kotlin documentation provides a full overview, including examples, of all the changes in this release.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT