I have a question about a behavior around `value c...
# getting-started
s
I have a question about a behavior around `value class`es. From my understanding they’re (in most cases) a zero-overhead way to make a “type-safe primitive”. But what happens in the case of collections and moving between the primitive and the value class?
Copy code
value class Box(val str: String)
val letters: List<String> = listOf("A","B","C",...)

val boxes: List<Box> = letters.map(::Box)
val backToLetters: List<String> = boxes.map(Box::str)
Are there any costs associated with moving between a
List<String>
and
List<Box>
and vice versa?
e
yes, that boxes/unboxes each element, as of it were a non-value class
🙏 1
👍 1
z
since the value itself would be inlined, wouldn’t the identity map be optimised away by the compiler resulting in a no-op?
e
no, inline classes have two representations
z
ah interesting thanks 👍
c
Note that this is the current behavior, but there are plans to eliminate those boxes in the future (thanks for Project Valhalla, for example)
🆒 1
s
you know that you forgot the
@JvmInline
infront of
value class
, don't you!?
👌 1
😅 1