Contracts are coming to Kotlin 1.3

Kotlin 1.3-M2 is here. In addition to improving the stability of the features announced in Kotlin 1.3-M1, this new milestone release introduces contracts, a new experimental addition to the Kotlin type system.
Kotlin 1.3-M2 arrives with lots of improvements in tow, as well as a new experimental addition to the Kotlin type system called contracts.
Here are some of the highlights of the second milestone release:
-
Contracts improve smart-casts and other compile-time analyses
- New Standard Library functions for unsigned types and collections
- Reflection for coroutines
- A migration layer to aid migration onto new coroutines being graduated in 1.3
- Numerous bugfixes related to inline classes
As usual, you’ll find the complete list of changes in the changelog.
SEE ALSO: A first look at Kotlin 1.3: Coroutines no longer experimental
What’s coming in Kotlin 1.3
New experimental addition to the Kotlin type system: Contracts
There’s a new experimental addition to the Kotlin type system and it’s called contracts.
Contracts support a way to explicitly express some aspects of function’s behavior, thus allowing programmers to cooperate with Kotlin compiler by providing it with additional guarantees, getting more complete and intense analysis in return.
JetBrains’ Ilya Gorbunov explained in a blog post announcing the new milestone release that “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’.”
Contracts enrich the type information available through the function signature (“I take a nullable list of T and return a Boolean”) with additional meanings useful at the call site, e.g. “I only return false for non-null lists”.
However, you should know that Kotlin standard library already has contracts added to a few functions and the experimental flag does not influence their performance. Right now, the Kotlin compiler uses contracts for two types of improvements:
- Making smart casts even smarter
- Analyzing variable initialization more accurately
Last but not least, since the contracts haven’t been verified at the declaration site, this is where you come into play to provide accurate information. Have a look at this KEEP for more details regarding syntax, general design overview, and compatibility guarantees.
Coroutines update
We talked about coroutines in an article about the previous milestone release when we mentioned that there was no migration support bridging the old experimental coroutines with new stable ones. That’s no longer an issue! As of 1.3-M2, you can simply call old suspend functions.
What’s more, Kotlin reflection now supports introspection for suspend
functions:
KCallable.isSuspend
KCallable.callSuspend
KCallable.callSuspendBy
The last two allow for calling suspend
functions via reflection, so they are marked suspend
.
Standard library
There are a bunch of improvements to the unsigned integer types. Let’s have a look at the changes:
- Functions for converting unsigned integers to and from strings in an arbitrary base:
UInt.toString(base)
,String.toUInt(base)
etc. - Extension functions
until
,downTo
,step
andreversed
to support more ways of creating ranges and progressions of unsigned integers. - The arrays of unsigned integers do not implement the structural equality contract of
List
, same as the arrays of signed integers. To make it easy comparing these arrays or showing their contents as string thecontentEquals(other)
,contentHashCode()
,contentToString()
functions were introduced. - Secondary constructors of unsigned arrays to create a zero-initialized array of unsigned integers of a given size.
- Extension functions to copy unsigned integer arrays and their subranges:
copyOf()
,copyOf(newSize)
,copyOfRange(fromIndex, toIndex)
. - Extensions for converting a specialized array of unsigned integers to an object array:
UIntArray.toTypedArray()
which returns anArray<UInt>
, etc
We’ve only scratched the surface so if you want to see all the goodies in Kotlin 1.3-M2, read Ilya Gorbunov’s blog post.
Get started with Kotlin 1.3
In Maven/Gradle: Add http://dl.bintray.com/kotlin/kotlin-eap
as a repository for the build script and your projects; use 1.3-M2
as the version number for the compiler plugin and the standard library.
In IntelliJ IDEA: You’ll have to go to Tools → Kotlin → Configure Kotlin Plugin Updates, then select “Early Access Preview 1.3” in the Update channel drop-down list, and click Check for updates.
You can download the command-line compiler from the Github release page.
On try.kotlinlang.org: Use the drop-down list in the bottom right-hand corner to change the compiler version to 1.3‑M2.
asap