Question about value classes (I'm familiar with th...
# announcements
s
Question about value classes (I'm familiar with the old inline classes but it has been a while): I was under the impression value classes, like inline classes, behave with respect to the compiler like the wrapped class. So why is the following invalid?
Copy code
@JvmInline
value class Bar(
   val foo: Foo
)

fun Foo.doSomething() {
}

ERROR HERE >>> Bar(Foo()).doSomething()
Nevermind seems type aliases are better for this use case.
Still I could have sworn that inline classes transparently inherited all of the behavior and could otherwise replace their wrapped type.
l
The annotation is named JvmInline, not JvmValue
s
I think I made a mistake in the original question, in my code with JvmInline it still doesn’t seem to work.
r
Yes: inline/value classes are meant for wrapping a class so it behaves *differently*; but they try to minimize performance impact of the wrapping. Eg compiler will behave to it like a new class, with no real relation to wrapped one (except property ofc), but where it can, it will use just use the wrapped one so it doesn't actually need to do the wrapping.
n
I don't understand the question. Value/inline classes are supposed to create distinct types w/o runtime overhead.
doSomething
is only defined on
Foo
and not on
Bar
. So why do you expect
Bar(Foo()).doSomething()
to work?
s
@nkiesel My memory of how these worked was wrong.