Justin Tullgren
10/14/2024, 4:54 PMinterface Value // has semantic meaning beyond primitive types and could be an object in other contexts.
sealed class ValuePrimitives : Value {
data class ValueInt(val value: Int): ValuePrimitives()
data class ValueString(val value: String): ValuePrimitives()
}
I tried a custom serializer using the PrimitiveKindSerializer but I end up getting an error: Serializer for ValueInt of kind INT cannot be serialized polymorphically with class discriminator.
CLOVIS
10/15/2024, 7:28 AMValue classes do not work as they cannot implement interfaces.Value classes can implement interfaces.
@JvmInline
value class Bz(val bar: Int) : Comparable<Bz> {
override fun compareTo(other: Bz): Int =
bar.compareTo(other.bar)
}
Ulrich Schuster
10/15/2024, 11:54 AMtype
field because value classes only have a single propertyJustin Tullgren
10/15/2024, 11:55 AMJustin Tullgren
10/15/2024, 11:56 AMobject Serializer : KSerializer<ValueInt> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("ValueInt") {
element("value", serialDescriptor<Int>())
}
// override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ValueInt", <http://PrimitiveKind.INT|PrimitiveKind.INT>)
override fun serialize(encoder: Encoder, value: ValueInt) = encoder.encodeInt(value.value)
override fun deserialize(decoder: Decoder) = ValueInt(decoder.decodeInt())
}