sealed interface Color {
@JvmInline
value class StringColor(val value: String) : Color
@JvmInline
value class IntColor(val value: Int): Color
}
๐ 1
Kshitij Patil
10/06/2023, 8:17 AM
Was trying to avoid boxing-unboxing. It appears sealed & value classes are negating each other and boxing happens nonetheless. So no point doing this it seems
Your code will work, but yes, autoboxing can always happen.
I really like this guide for explaining when autoboxing happens https://typealias.com/guides/inline-classes-and-autoboxing/
To best avoid autoboxing you'd want something like a single Color value class, but with multiple constructors for string/int values. But what you've written works fine too, and autoboxing is unlikely to be a big problem, so I'd be more cautious about preoptimising!
k
Kshitij Patil
10/06/2023, 8:39 AM
But weโre not gaining anything with value class either