Skip to content

Support for inline classesΒ #199

@jnizet

Description

@jnizet

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions