Use Cases
Domain modeling.
Modeling the Domain
Value classes shine in domain modeling. They turn anonymous primitives into meaningful, mistake-proof types without runtime cost.
Let us walk through realistic scenarios.
Typed Identifiers
Distinct ID types prevent passing a user id where an order id belongs — a common and costly bug.
@JvmInline
value class UserId(val value: Long)
@JvmInline
value class OrderId(val value: Long)
fun fetchUser(id: UserId) = "user " + id.value
fun main() {
println(fetchUser(UserId(7)))
}