0Pricing
Kotlin Academy · Lesson

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)))
}

All lessons in this course

  1. Type Aliases
  2. Inline Value Classes
  3. Use Cases
  4. Limitations
← Back to Kotlin Academy