How can I pass value class parameter from Java? ID...
# getting-started
l
How can I pass value class parameter from Java? IDEA suggests me using the underlying type but it's just a compilation error.
oh, constructors are a bit trickier since they can't be renamed
l
oh thanks 😄, it's just a normal function.
m
Another alternative is to wrap it in a
sealed interface
and use an invoke operator to instantiate it. So from Java it'll be
MySealedValueClass.invoke("some value")
, and from Kotlin it'll just be
MySealedValueClass("some value")
https://kotlinlang.slack.com/archives/C3PQML5NU/p1651526497807819?thread_ts=1651318218.682619&cid=C3PQML5NU
e
that loses any benefit of
value class
though; by using the interface it will always be boxed
1
m
Created a library for this specific issue of Kotlin
value class
not compiling to platform code. https://github.com/05nelsonm/component-value-clazz
e
that isn't a
value
at all though?
m
nope. It's just an abstract class that overrides
hashCode
,
equals
and
toString
which your implementation would extend. Functionally, it does the same thing as a Kotlin
value class
if that
value class
were to implement an
interface