![]() Digging into the source code of Kotlin's compiler, one can find various flavors of the nullability annotation that aims to achieve this. The way to leverage the existing null safety mechanism is by representing platform types as an actual Kotlin type (nullable or non-nullable). The way to declare it is as below (looks almost the same as Swift, except for the nil isn’t it) val number. When we try to invoke Java code from Kotlin, null checks are relaxed due to platform types. In Kotlin, the new type is called Nullable. Because they aren’t wrapped in a class, getting at the actual value is easy. ![]() The following mnemonic notation can be used to denote them: Kotlin’s nullable types have many distinct advantages over Optional. Hence, there is no syntax in the language to represent them. E.g.įrom the Kotlin compiler perspective, it is a type that can be nullable and non-nullable. if we try to declare a variable of a platform type, we will receive a compilation error. Let’s have a look at how we can handle the nullability of platform types. These types cannot be mentioned explicitly in the program, i.e. Java's declarations are treated as platform ( flexible) types in Kotlin. In this post, we will focus on how to take advantage of null safety when invoking Java code.
0 Comments
Leave a Reply. |