zoqaseal.blogg.se

Kotlin is null
Kotlin is null













kotlin is null

Therefore, constructor of Mercedes references a yet-uninitialized LewisHamilton variable, and leads to a null value being stored in a non-nullable variable. Its constructor references another lazy variable Mercedes, whose constructor has a circular reference to LewisHamilton. LewisHamilton gets initialized upon its first-access in main.

kotlin is null

If you run this example on Kotlin Playground, it produces the following output: Driver(name=Sir Lewis Hamilton, teamId=merc)Ī null value snuck into a non-nullable type. Val Mercedes: Team = Team( "merc", listOf(Drivers.LewisHamilton))Īlthough innocuous at first glance, this code breaks Kotlin's non-nullable types. Val LewisHamilton: Driver = Driver( "Sir Lewis Hamilton", ) They break code at runtime, and are difficult to guard against at compile time.Ĭonsider the following example: data class Driver( val name: String, val teamId: String)ĭata class Team( val id: String, val drivers: List) So Jackson deserializer seems to handle this correctly, whereas Fauna Java driver deserialization does not.Breaking Kotlin's Null-Safety with Circular ReferencesĬircular object references can be dangerous. If we compare to the Jackson deserialize/serialize library that is used to deserialize the request body from the frontend to my backend service, this is not a problem. Could this be it? In Kotlin, when a field is non-nullable, the field needs to be set upon object creation (providing the value to the constructor normally). The problem seems to lie in that the deserialization process first instantiates the data class with no values (because this is OK in Java) and then sets the values on every field afterwards. It is a string value and it is correctly returned from Fauna and received in my application. The value of the field in Fauna is not null. One of my Kotlin data classes has a field that does not allow null values - a feature of Kotlin language (see here: Null safety | Kotlin) - and thus causes an exception to be thrown when data from Fauna is fetched and deserialization process and creation of instance of Kotlin data class is attempted. I’m writing a backend service in Kotlin and Spring Boot, and for Fauna integration I’m using the Java driver ( GitHub - fauna/faunadb-jvm: Scala and Java driver for FaunaDB).















Kotlin is null