Kotlin 1.3 is near… Here’s what’s coming

Serhii Prodan
5 min readOct 9, 2018

--

This is my first post in Medium. I’ve promised myself for many years to start writing and I think the time has finally come. Please bear with me :)

Kotlin 1.3 is closing in and with it a number of improvements and new features. Let’s see what will the next release of our beloved language bring.

Stable Coroutines

Coroutines provide a powerful paradigm to design asynchronous, non-blocking programs. Essentially coroutines are lightweight threads allowing for asynchronous (non-blocking) programming.

The coroutine API appeared in 1.1 and has been in experimental state since then. However, JetBrains are finally releasing the coroutines API and from 1.3 it will remain stable along with other released functionality. Essentially it means that the API will not change in the future releases and can be used in production.

Check out their brand new coroutines page for lots of docs and tutorials.

Contracts

Kotlin 1.3 introduces an experimental feature called contracts. One of the most notable features where contracts are used is Kotlin’s smart casts.

Consider the following code:

fun foo(s: String?) {
if (s != null) s.length
}

Here the compiler automatically casts s to String. However, when this check is done outside of the scope of the function:

fun String?.isNotNull(): Boolean = this != nullfun foo(s: String?) {
if (s.isNotNull()) s.length // No smartcast :(
}

Contracts allow to describe behavior of functions which will be understood by the compiler. Sounds pretty cool, doesn’t it?

Kotlin stdlib already makes use of contracts and, more importantly, this part of contracts is stable. Consider the following:

fun bar(x: String?) {
if (!x.isNullOrEmpty()) {
println("length of '$x' is ${x.length}") // Smartcast to not-null!
}
}

isNullOrEmpty is a part of stdlib and has a contract implemented in the function:

public inline fun CharSequence?.isNullOrEmpty(): Boolean {
contract {
returns(false) implies (this@isNullOrEmpty != null)
}
return this == null || this.length == 0
}

It is possible to declare custom contracts for your own functions, but bear in mind that this feature is experimental. Also, the Kotlin compiler does not verify contracts as of now and you are responsible for writing correct contracts. You can refer to this KEEP for more information and syntax.

Capturing when subject in a variable

As of Kotlin 1.3 it is possible to capture subject of when in a variable and do something like this:

when (val statusCode = request.statusCode) {
in 200..204 -> println("Response code is success: $statusCode")
in 400..451 -> println("Client error response: $statusCode")
else -> println("Unknown HTTP status code")
}

Note that scope of the captured variable is restricted to the when body.

This might seem like a small feature, but it could save you some unnecessary declarations and restrict the scope of the variable if needed.

Annotation classes can have nested declarations

Previously annotation classes in Kotlin could not have nested declarations. As of Kotlin 1.3 nested classes, interfaces, objects and companion objects are supported. This is a very great feature as it will be possible to finally put some logic inside the annotation classes like so:

annotation class Foo {
enum class Direction { UP, DOWN, LEFT, RIGHT }

annotation class Bar
companion object {
fun foo(): Int = 42
val bar: Int = 42
}
}

Main without parameters

Kotlin 1.3 introduces a simpler form of main entry point which does not have parameters. Many applications do not use the parameters from main hence you could save yourself some keystrokes:

fun main() {
println("Hello, world!")
}

Inline classes

Another cool feature is inline classes, which in many cases should optimize the performance of your application.

If you declare your class as inline, which is basically a restricted version of a regular class (namely, inline classes mush have exactly one property) Kotlin compiler will try to optimize the runtime representation of the class and substitute its instance with the value of the underlying property of inline class.

inline class Name(val name: String)fun main() {
val name = Name("Kotlin")
println(name.s)
}

Here no constructor call happens when an instance of Name is created, and at runtime name contains only string “Kotlin”.

Improvements to stdlib

There are several notable improvements made to stdlib. Check out here for more info.

Tooling updates

Official code style support in IDE.

Kotlin finally comes with a pre-defined code style support available in the IDE. In an essence it means you can follow the official code style without the need to go into your settings and modifying the code style manually, or using some third-party solutions. The code style will be available in the IDE settings and switching to the Kotlin Coding Conventions code style can be done in Settings → Editor → Code Style → Kotlin dialog. Refer to code style migration guide for more information.

Scratches support

As of Kotlin 1.3 you can run Kotlin scratch files (script file with a .kts extension) which can be run and evaluated directly in the IDE.

These are some, but not all of the features and updates brought by the next Kotlin release. I have decided to highlight the ones that caught my eye and which I will start using on a regular basis. Up next I plan to go over some of the new features like Contracts and Inline Classes and explore them more deeply.

For a full reference of new features coming in Kotlin 1.3 refer to the official page.

Google teams up with JetBrains

On a side, but related note, a few days ago, during KotlinConf 2018, Google and JetBrains announced that they are joining forces and launching the Kotlin Foundation. The foundation will ensure that Kotlin will continue to advance rapidly, remain free and open, and can be relied upon for years to come.

The mission of the Kotlin Foundation is to protect, promote and advance the development of the Kotlin programming language. The foundation secures Kotlin’s development and distribution as Free Software, meaning that it is able to be freely copied, modified and redistributed, including modifications to the official versions.

Google Cloud Platform team has also launched a dedicated Kotlin portal. This will help developers more easily find resources related to Kotlin on Google Cloud.

These are exciting news as it brings more exposure for the language as well proves it that Kotlin is here to stay for long.

--

--

Serhii Prodan

Searching for the answer to the Ultimate Question by night, tester by calling, serendipitously became a devops lead by day. Automating things I get my hands on.