
In this post we will see how the Optional type compares and translates to nullable types in Kotlin. Kotlin natively supports nullable types, making the Optional type, as well as all the API it provides, obsolete. Initially I was surprised that Kotlin did not embrace the Optional type like Scala did. When I learned about Kotlin last year, it seemed to have some similarities to Scala, making me very excited. I already knew about the Optional type from various adventures into functional programming languages and knew of its powers. Of Java method which was not generated by Kotlin compiler.When Java 8 introduced the Optional type many years ago, I was happy like a bird. In essence platform type is used every time Kotlin compiler encounters an invocation To resolve this problem Kotlin introduces the concept of platform types. The case of the result returned from Java method where Kotlin compiler has to take some precautions.Īfter all it does not know if the value returned can be null or not. Is a difference when we want to call Java code from Kotlin. Of view with regard to null-safety nothing unusual happens if we call Kotlin code, there So far we have not talked about the interoperability with Java. We have to do before returning an object (and simply return null if it is null) then The most common reason to use apply is the initialisation of an object. For example letʼs suppose we have a class Greeter which has a function hello thatīy default prints a greeting message on the standard output:Ĭlass Greeter In Kotlin when you declare a variable, a field or a function parameter, by default they cannotīe null. This problem is to use some kind of Option monad.Īs weʼll soon see Kotlin actually does not introduce any new special wrapper type - it uses regular Java It is quite interesting - especially at times like these when the most popular way of handling

One of the first features a Kotlin developer learns is the languageʼs approach to handling null values. Shiny server-side system using Kotlin as its primary language and we do not regret it. Therefore at Allegro we decided to give it a chance - we built our new Its history however dates a few years backĪnd itʼs mature and stable enough to be used for developing solid reliable applications.

Kotlin may seem like a new kid on the block - itʼs been officially released only in February.
