```@JvmInline value class Foo(id: String) val foo...
# server
k
Copy code
@JvmInline value class Foo(id: String)

val foo = Foo("world")
println("hello $foo")

>> "hello Foo(id=world)"
d
This is expected as all methods on the value class will effectively be inlined by the compiler
k
I would of expected the output to be
hello world
. Anyways, thank you
d
np. 🙂 . The compiler is being consistent - if you think that to all intents and purposes, your Kotlin code is treating the value class as an actual class - it's just the compiled code in which it's just a primitive. Thought experiment: if you wanted to override the toString method and it didn't do this, how would you do that? 🙂
k
I was under the expectation that a value class just added an extra type layering for strict type-safety and that at compile time it just ripped it completely away (basically completely transparent). Now that you mention consistency, it does make sense that it does what it does. I never thought about that
d
It's more like a compiler optimisation (or compiler hack if you were being unkind 😉)
The same type of thing happens all over the place in the compile step - eg. coroutines or properties. The transformation is done in that low layer so you (the dev) doesn't have to worry about it.
k
Thanks @dave!
c
Additionally, you can override
toString
:
Copy code
@JvmInline value class Foo(id: String) {
    override fun toString() = id
}