
It kills one major benefit of using Kotlin.

This was a big mistake when languages like C. Null Safety In Java, all objects were allowed to be nullable. It is very error-prone as plain old NPE may appear at run-time again. I consider Kotlin to be a very superior language, and it is also my favourite. It is nothing more than a way to stop using all the benefits of Kotlin type system. In object-oriented languages, access to objects is achieved. The !! operator in Kotlin allow the programmer to tell the compiler “Ok, I know what I do, and I promise, it won’t be null”. Null Safety (or void safety) is the guarantee that no object reference will have a null value. lateinit: In case of late-initialization (like in a test), there is a specific language keyworkd lateinit which allow to declare a non-null member without initializing it in the constructor.Īll of that makes dealing with nullability really great in Kotlin.

Therefore the Kotlin compiler is able to smart-cast. Contracts: requireNotNull(x) x.lenght compiles, because requireNotNull defines a contract stating that if the function does not throw, it means the argument is not null.Smart-cast: if (x != null) x.length compiles, because the Kotlin compiler knows that it cannot be null when length is called.It can also be used to return the function or throw an exception. Elvis operator: val x: String = input ?: "default" allow to choose what to do if something return null.

