-
-
Notifications
You must be signed in to change notification settings - Fork 180
Closed
Labels
Description
Kotlin 1.3 has experimental inline classes.
When wrapping a primitive type, an inline class instance is just compiled to the primitive type, making the code very efficient, whie still keeping it typesafe and preventing for example to sum Watts with Volts.
When the inline class instance is nullable, a wrapper class is used (just like java.lang.Integer is used to represent a nullable Int).
Unfortunately, inline classes don't follow the same rules as standard wrapper classes. Example:
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
inline class Watt(val value: Long)
data class Foo(val nonNullable: Watt, val nullable: Watt?, val otherNullable: Watt?)
data class Bar(val nonNullable: Long, val nullable: Long?, val otherNullable: Long?)
fun main() {
val foo = Foo(Watt(1000), Watt(2000), null)
val bar = Bar(1000, 2000, null)
val objectMapper = ObjectMapper().registerModule(KotlinModule())
println("With Watt: ${objectMapper.writeValueAsString(foo)}")
println("With Long: ${objectMapper.writeValueAsString(bar)}")
}
The output of this program is:
With Watt: {"nonNullable":1000,"nullable":{"value":2000},"otherNullable":null}
With Long: {"nonNullable":1000,"nullable":2000,"otherNullable":null}
It would be really nice if the first output was identical to the second one, and of course if unmarshalling worked too.
TjeuKayim, functionaldude, antonveretennyk, Yona-Appletree, jivimberg and 85 moreGounlaf and dzikoysk