There’s a great little feature in the Kotlin v1.3 that needs more attention, and that is, Kotlin Contracts!

Kotlin Contracts

To use it in your project, update you Kotlin Gradle plugin to version 1.3 (or above):

Enable Kotlin eap in gradle

Contracts (or should we say, Kontracts?)

Kotlin contracts is a great way to tell the compiler about what a function does and help it become more intelligent.

1. CallsInPlace and InvocationKind

Let’s say we have a function as below.

Kotlin Contract — What it fixes

In this example, we have a function createOnce that we know, runs the runFunction exactly once. We use this function in the getKotlinVersion and try to initialize a val with the above function, but then, we are presented with the following compile error:

Captured values initialization is forbidden due to possible reassignment

Kotlin compiler is telling us that it is not sure about the createOnce function, and that it might run the runFunction more than once (or actually, never), and since we are trying to assign a value to a val , that cannot be allowed. Another thing to note is that the runFunction has to be called exactly once, not more and not less.

To fix this problem and give the compiler clues about what a function does, we can use contracts.

By adding the contract block to the first line of the function, the compiler can get more information about this function. Here we tell the compiler that the runFunction will be run exactly once, and since the compiler knows this, it’s not gonna throw an error in the getKotlinVersion !

There are other kinds of InvocationKind like AT_LEAST_ONCE and AT_MOST_ONCE which are self explanatory.

Lets explore another aspect of this feature.

2. Returns and implies

In the example above, we have a function for checking of a name’s validity that returns true If that object is a String and has a length more than 3.

In the first file, the compiler throws an error, even though the function has returned true (and has passed the is and the !=null check).

To fix this we can add another type of contract with the returns keyword that tells the compiler that if this function returns true, then that implies that this object is not null and is a String. So when we pass the if test on isAValidName , The compiler can make sure that the name is a String and casts it to String .

returns accepts true , false and null as an input and implies accepts a Boolean as the input. One thing to note is that the condition inside implies can only access the property ( this in this case) and the arguments ( runFunction in the first example).

The complete information about contracts is presented in this KEEP.

And that’s it for Kontracts! Hope you enjoyed its API as much as I did.

Follow me on Medium if you’re interested in more informative and in-depth articles about Kotlin and Android 🍻.