Inline Value Classes
Wrap without overhead.
The Wrapping Problem
Wrapping a primitive in a class adds type safety but normally costs a heap allocation. Value classes give you the safety while the compiler erases the wrapper where possible.
Declaring a Value Class
Mark a class @JvmInline value class with exactly one property in the primary constructor. It behaves like a distinct type.
@JvmInline
value class Password(val value: String)
fun main() {
val p = Password("secret")
println(p.value)
}All lessons in this course
- Type Aliases
- Inline Value Classes
- Use Cases
- Limitations