Java Interoperability question: Can’t we access In...
# announcements
g
Java Interoperability question: Can’t we access Inline Value class fields and constructor from Java? In the below example I don’t get any compiler error but fails at runtime with
error: cannot find symbol
# Kotlin Value class
Copy code
import io.vavr.control.Either
import io.vavr.kotlin.right

@JvmInline
value class Name(val s: String) {
    val length: Int
        get() = s.length
}

fun getAsName(): Either<String, Name> = right(Name("Kotlin"))
# Java Client
Copy code
@Test
void testValueClass() {
  final var name = getAsName().get();
  Assertions.assertEquals(0, name.getLength());
}
# Error
Copy code
error: cannot find symbol
    Assertions.assertEquals(0, name.getLength());
                                   ^
  symbol:   method getLength()
  location: variable name of type Name
r
I would assume that works the same for members
g
hey @rnett thanks, but @JvmName is not applicable for fields
r
Can you use
@get:JvmName
? Or
@property:
?
g
Do u mean something like this?
r
Yeah, I guess that doesn't work. I'm not sure what the "correct" way is, but for now you could just make an extension or helper method to use from Java.
g
ya, that’s what I am planning, but from java, extensions fns calling isn’t fluent, I need to call like
fn(thisObj)
which hurts the API I am building using value classes
g
For now, there is no official way to access it from Java
g
Thanks a lot Andrey