![MMS Founder](https://mobilemonitoringsolutions.com/wp-content/uploads/2019/04/by-RSS-Image@2x.png)
MMS • RSS
Article originally posted on InfoQ. Visit InfoQ
![](https://mobilemonitoringsolutions.com/wp-content/uploads/2018/10/logo-big-9.jpg)
At KotlinConf 2018 in Amsterdam, Netherlands, JetBrains has announced Kotlin 1.3 RC, introducing new language features such as stable coroutines, contracts, inline classes, and more. At KotlinConf, Google and JetBrains also announced their joint effort to establish the Kotlin Foundation.
Coroutines provide a powerful paradigm to design asynchronous, non-blocking programs. A coroutine is just a light-weight thread that is spawned using launch in a CoroutineScope. The following is a minimalist example of a coroutine that carries through some concurrent processing and then joins with the launching thread. Notice the use of runBlocking
to wrap the coroutine and the block that launches and waits on it, which defines the coroutine scope:
fun main(args: Array<String>) = runBlocking {
val job = GlobalScope.launch {
// do something on a background thread
}
println("Hello,")
job.join() // wait until child coroutine completes
}
Coroutines can be suspended, restarted, and composed. Coroutines were already available in previous Kotlin releases, but version 1.3 makes them stable, meaning that their API will not change in future releases.
Contracts are a new experimental feature added to Kotlin’s type system to describe additional guarantees on top of those represented through a function’s signature. JetBrains engineer Ilya Gorbunov describes coroutines as a means to enrich the type information available through the function signature with constraints that are useful at the calling site:
With contracts, a function can tell the compiler things like “I affect smart casts this way” or “I execute this lambda immediately and exactly once” [or] “I only return false for non-null lists”.
Contracts are currently used to improve smart casts and to analyze variable initialization more thoroughly:
fun test(x: List<Int>?) {
// If the function returns false, the value is definitely not null:
if (!x.isNullOrEmpty()) {
println(x.size) // Yay, smart cast to not-null!
}
}
fun test(x: Any?) {
// If the function returns (does not throw), then the argument is true:
require(x is String)
println(x.length) // Smart cast here too!
}
val x: Int
synchronized(lock) {
x = 42 // The compiler knows that the lambda is invoked exactly once!
}
println(x) // Allowed now, the compiler knows that 'x' is definitely assigned.
Inline classes are another experimental feature introduced in Kotlin’s latest release. Inline classes are classes that have exactly one property:
inline class Name(val s: String)
They are useful to prevent signature clashes among overloadsq, as in the following example that shows three overloads using different inline types all mapped to String
:
fun foo(x: UserName) { ... }
fun foo(x: Login) { ... }
fun foo(x: UserHash) { ... }
A new feature that might appeal to all developers moving their first steps in Kotlin is the revamped online playground, which, besides a new look, also provides a new “Kotlin by Example” section.
The official Kotlin announcement is worth reading in full if you are interested in all new features and improvements brought by Kotlin 1.3, which are many more than what could be covered here.
On a related note, Google and JetBrains announced the Kotlin Foundation, aimed to advance the development of Kotlin while ensuring it remains free and open. Its establishment comes two years after Google made of Kotlin a first-class language for Android development and started bringing a number of improvement to its mobile platform to offer developers a better experience when using Kotlin for Android app development. As a result of this effort, Google says, 27% of the top 1000 Android apps on Google Play already use Kotlin.
Using Kotlin 1.3 RC is extremely easy if you use JetBrains IntelliJ IDEA, which has built-in support for Kotlin updates. If you use Maven/Gradle, you should add the https://dl.bintray.com/kotlin/kotlin-eap
repo in your configuration and then get version 1.3.0-rc–57.