What’s New In Kotlin 1.2 | 8 Added Features and Changes

First appeared in 2011, the statically-typed programming language Kotlin is now picking up speed – it is getting popular day-by-day. As per data and predictions, more than 20 percent of the applications built with Java before Google I/O 2017 are being built within Kotlin language. In future, it might even change how Java is used on the server. It is expected that Kotlin will surpass Java in December 2018.

The beta version of Kotlin 1.2 is out. No surprise, it focuses on stability and bug-fixes in compiler and tooling as well as enhancements of the Kotlin standard library. Let’s find out what changes have been made and what features they have added.

8. Treat Warnings as Errors

The Kotlin compiler gives you a choice to treat all program warnings as errors. Just add the snippet shown above, or go to the command line and type -Werror.

7. Improvements In Type Inference

The compiler now uses type casts information in type inference. If you call a generic method, which returns a type parameter S and cast the return value to particular type XYZ, the compiler automatically knows that S for this call should be bound to the type XYZ – an important feature for Android developers as from now onwards, Kotlin compiler can appropriately analyze findViewById calls in Android API Level 26.

6. Short-Hand Syntax For Bound Callable Reference

In Kotlin 1.2, you can eliminate this in expression such as this::doWork that generates callable references bound to a member of this. Now you only need to write ::doWork

5. New Package

The standard library of Kotlin is now completely compatible with Java 9 module system. They have added a new package named kotlin.math for executing mathematical operations in cross platform code.

The Kotlin 1.2 now supports inverse hyperbolic functions – acosh, asinh and atanh. Also, functions for presenting floating point numbers to binary, such as nextUP and toBits, are available for JavaScript.

4. Better Smart Casts

Kotlin 1.2 supports smart casting of subjects of safe casts (and also safe call’s receiver)

Furthermore, smart casts are allowed in closure if a local variable is modified before it (not inside or after).

3. Upgraded lateinit

Kotlin 1.2 comes with a new reflection API that lets you verify whether a lateinit variable has been initialized.

You can also use lateinit modifier on local variables and top-level properties. For instance, you can use it while initializing an object graph when objects in the graph have circular dependencies.

2. Array Literals In Annotations

Kotlin now supports array literals in annotations. For instance, rather than writing @CacheConfig(cacheNames = arrayOf(“computers”, “tablets”)), you can simply write a literal expression @CacheConfig(cacheNames = [“computers”, “tablets”]).

Developers have made the syntax more consistent – now you can use array literals for array as well as vararg parameters.

@RequestMapping(value = [“12345”, “54321”], path = [“pathA”, “pathB”])

1. Multiplatform Projects

This is a new experimental feature that allows users to reuse code between supported target platform – JavaScript and JVM. The code is shared among platforms into a common module, and platform depended parts into platform depended module. The code for both common and platform-specific parts is produced when a user complies this type of project in a particular platform.

Read: What’s New In Java 9 | 19 Added Features and Changes

Multiplatform project is capable of expressing dependencies of common code on platform-specific parts via actual and expected declarations. An actual declaration is either a typalias that refers to an existing implementation of external library’s API or a platform-dependent implementation. Whereas, expected declaration defines an API (annotation, interface, class, and more).

If you have already tried this feature, you need to replace header and impl with expected and actual.

Leave a reply