Kotlin 1.2 Beta released

Beta version of Kotlin 1.2 available now with some new interesting features
03 October 2017   449

Good news for all Kotlin enthusiasts and Android developers. New Beta version of Kotlin was recently released.

What's new in Kotlin 1.2 Beta?

According to the developers, the major feature of version 1.2 is experimental support for multiplatform projects. Additionally, the language and standard library are now feature complete – all the new features planned for Kotlin 1.2 have been implemented.

In terms of tooling, Kotlin 1.2 Beta includes the same set of features as the recently released 1.1.50 update. The beta is compatible with all versions of IntelliJ IDEA from 2016.3 until 2017.3, as well as with Android Studio 2.3 and 3.0.

So, let's check the most interesting changes

Multiplatform projects

Multiplatform projects are a new experimental feature in Kotlin 1.2, that allows you to reuse code between target platforms supported by Kotlin – JVM, JavaScript and (in the future) Native. In a multiplatform project, you put code which is shared between platforms into a common module, and platform-dependent parts into platform-specific modules that depend on it. When you compile such a project for a specific platform, the code for both the common and platform-specific parts is generated.

A key feature of the multiplatform project support is the possibility to express dependencies of common code on platform-specific parts through expected and actual declarations. An expected declaration specifies an API (class, interface, annotation, top-level declaration etc.). An actual declaration is either a platform-dependent implementation of the API or a typealias referring to an existing implementation of the API in an external library:

// Common code
expect fun hello(world: String)
expect class URL(spec: String) {
  open fun getHost(): String
  open fun getPath(): String
// JVM code
actual fun hello(world: String) {
  println("Hello JVM $world")
actual typealias URL = java.net.URL

Array Literals in Annotations

A new language feature in Kotlin 1.2 is the support for array literals in annotations. Now, instead of writing something like @CacheConfig(cacheNames = arrayOf("books", "default")), you can simply use a literal expression:

@CacheConfig(cacheNames = ["books", "default"])

lateinit improvements

Developers added a new reflection API allowing you to check whether a lateinit variable has been initialized:

lateinit var file: File
// ...
if (::file.isInitialized) {

Bound callable reference improvements

You can now omit this in expressions like this::foo, which create callable references bound to a member of this. Instead, you can simply write ::foo. Previously the syntax with the empty left side could only be used to create callable references to top-level declarations. 

Type inference improvements

The Kotlin compiler can now use information from type casts in type inference. If you’re calling a generic method that returns a type parameter T and casting the return value to a specific type Foo, the compiler now understands that T for this call needs to be bound to the type Foo. This is particularly important for Android developers, because the compiler can now correctly analyze findViewById calls in Android API level 26:

val button = findViewById(R.id.button) as Button

As the method has been changed to <T extends View> T findViewById(int id), Kotlin 1.1 was unable to infer the type argument for T in such calls.

Warnings as errors

The compiler now provides an option to treat all warnings as errors. Use -Werror on the command line, or the following Gradle snippet:

compileKotlin {
  kotlinOptions.warningsAsErrors = true

Smart cast improvements

Smart casts are now applied to subjects of safe casts:

fun foo(x: Foo?) {
  val b = (x as? SubClass)?.subclassMethod1()
  if (b != null) {
    x.subclassMethod2() // x is smart cast to SubClass

Also, smart casts in a lambda are now allowed for var variables that are only modified before the lambda:

var x: String? = null
if (flag) x = "Yahoo!"
run {
  if (x != null) {
    println(x.length) // x is smart cast to String


kotlin.math is a new package in the Kotlin 1.2 standard library, allowing you to perform mathematical operations in cross-platform code. In 1.2-Beta, we made several improvements to it:

  • Inverse hyperbolic functions (asinhacoshatanh) are now supported
  • Functions related to binary representation of floating point numbers (toBitsnextUp and so on), are now available for JavaScript
  • Improved precision of math polyfills for JavaScript

How to do try Kotlin 1.2 Beta?

In Maven/Gradle: Add http://dl.bintray.com/kotlin/kotlin-eap-1.2 as a repository for the build script and your projects; use 1.2.0-beta-31 as the version number for the compiler plugin and the standard library.

In IntelliJ IDEA: Go to Tools → Kotlin → Configure Kotlin Plugin Updates, then select “Early Access Preview 1.2” in the Update channel drop-down list, then press Check for updates.
The command-line compiler can be downloaded from the GitHub release page.

On try.kotlinlang.org: Use the drop-down list at the bottom-right corner to change the compiler version to 1.2‑Beta (coming soon).

Learn more at official blog.

How to convert list to map in Kotlin

Two ways to convert list to map in Kotlin, with code examples
31 October 2017   512

For example, you have a list of strings like:

val list = listOf("a", "b", "c", "d")

and you need to convert it to a map, where the strings are the keys.

There are two ways to do this:

The first and most performant is to use associateBy function that takes two lambdas for generating the key and value, and inlines the creation of the map:

val map = friends.associateBy({it.facebookId}, {it.points})

The second, less performant, is to use the standard map function to create a list of Pair which can be used by toMap to generate the final map:

val map = friends.map { it.facebookId to it.points }.toMap()