Kotlin 1.6.0 released

JetBrains released Kotlin 1.6.0 which ships with the standard language and library features of 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) : Employee()
data class Operations(val operatingSystems: List) : Employee()
}
fun printInformation(employee: Employee) {
when (employee) {
is Employee.Developer -> printLanguages(employee.languages)
}
}
Kotlin provides already verified when
expressions for sealed classes, enumerations, and Boolean types for completeness. Use a non-exhaustive list when
The instruction 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 implementation suspend
functional types as supertypes. Previously, it was only possible to use lambdas and suspension function references for the desired behavior. Now a separate class can be used to implement the functional type of suspension:
class OrderManager : suspend () -> Unit {
override suspend fun invoke() { ... }
}
fun buy(action: suspend () -> Unit) {}
Conversions from regular functional types to suspended functional types are now implicitly converted for any expression form after being previously limited to functional literals and callable references. This allows a regular function to be used as a parameter for a type of suspension:
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. In the same way, readlnOrNull()
can be used as an alternative to readLine()
that comes back null
when the end of the file is reached.
The typeOf()
feature was already available as an experimental API for the JVM platform and is now officially available for all platforms.
Collection builders love buildList()
, buildMap()
and buildSet()
are now stable and return serializable collections in their read-only state.
API Duration toComponents()
the function now accepts a Long
instead of a 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 because it was not 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 rotations of integer bits, splitToSequence()
which divides a string based on a regular expression, Comparable.compareTo()
can 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 is possible to develop based on the three previous API versions and the current stable version which currently includes 1.3, 1.4, 1.5 and 1.6.
Kotlin’s @kotlin.annotation.Repeatable
is now compatible with Java and accepts retention and can be used repeatedly. Java repeatable annotation also works in Kotlin.
A new experimental version, which should be used for evaluation purposes only, of the memory manager is provided to remove technical differences between native platforms and JVM. This allows for a leak-free concurrent programming primitive without the need for annotations or other configuration on the part of developers. Kotlin / Native now also supports Xcode 13 and allows compilation of Windows targets on all hosts that support Kotlin / Native.
Node.js and Yarn downloads for Kotlin / JS projects can be disabled when they are 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, measuring code coverage was a challenge because, for example, JaCoCo was not integrated into the Gradle toolchain and cross-platform projects.
Kotlin’s documentation provides a full overview, including examples, of all the changes in this release.