Predict the output
Which call fails to compile?
@JvmInline value class UserId(val raw: String)
@JvmInline value class OrderId(val raw: String)
fun loadUser(id: UserId) = "user ${id.raw}"
fun main() {
println(loadUser(UserId("u1")))
println(loadUser(OrderId("o1")))
}Hint 1 of 1
Are UserId and OrderId the same type?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A loadUser(OrderId("o1"))
Correct. OrderId and UserId are different types.
Why not the others?
- B loadUser(UserId("u1")). That's exactly the type loadUser expects.
- C Neither: both wrap a String. Value classes are distinct types at compile time.
- D Both: value classes can't be function parameters. They can.