Kotlin 1.4-M2 brings standard library improvements

Next Kotlin 1.4 milestone release features API improvements and new functions for arrays and collections

JetBrains is working toward the general release of Kotlin 1.4, an upgrade to the open source, general purpose language for JVM, Android, web, and native development that promises improvements to the standard library including the API.

In a bulletin posted on May 12, JetBrains detailed standard library improvements in the forthcoming Kotlin 1.4-M2 milestone release. An early version has been deployed to the Kotlin playground for developers to try out. Changes to the existing API are featured, including updating signatures and introducing new constants. Some functions have been relaxed to accept nulls.

For example:

val s: String? = null
println(s.toBoolean())  // false

(Note that Kotlin 1.3 will not compile this code.)

Other API-related code changes also are featured, such as SIZE_BITS and SIZE_BYTES, which are new constants in Double and Float. These contain the number of bits and bytes used to represent an instance of the type in binary form.

Also new in the Kotlin 1.4-M2 standard library:

  • The common library is gradually being extended to add missing functionality. The standard library can be used in “common” code, i.e. the code shared between different platforms — Android, iOS, JVM, or JavaScript.
  • New array functions in the standard library include shuffle(), which puts array elements in a random order, and onEach(), which performs the given action on each array element and returns the array itself. Also, functions have been added for sorting array subranges.
  • The collections API has been expanded to cover more real-life cases. A new set creation function, set()NotNull(), makes a set consisting of all the non-null items among the provided arguments. Also, runningfold() and runningReduce() were introduced as synonyms for scan() and scanReduce(), to be more consistent with related functions.
  • With property delegation improvements, complementary interfaces will work better, with a new PropertyDelegateprovider interface and ReadWriteProperty, now inheriting ReadOnlyProperty. In addition, a property can delegate its getter and setter directly into another property.

The first milestone of Kotlin 1.4 was released on March 23 with access instructions. It featured a more powerful type inference algorithm and compiler generating type annotations in the bytecode for Java 8+ targets.

Copyright © 2020 IDG Communications, Inc.