1

I would like to create an object of:

class MyUIntValue(val v: UInt)

This works in kotlin:

fun main() {
  MyUIntValue(1U)
}

But in java code I get an error:

  public static void main(String[] args) {
    System.out.println(new MyUIntValue(1));
  }
error: MyUIntValue(int) has private access in MyUIntValue

I didn't find in the docs what's the proper way to create a UInt from java?

oshai
  • 14,865
  • 26
  • 84
  • 140
  • 2
    There are no unsigned types in Java. Please see https://stackoverflow.com/a/9854205/10514633 – Robin Oct 18 '22 at 10:28

1 Answers1

3

UInt is implemented as an inline class in Kotlin. Functions that accept inline classes are compiled with "mangled" names so they aren't normally callable from Java. You can disable the name mangling by giving the function an explicit @JvmName. I don't think you can do this for a class constructor, so you'd need to declare a separate function that calls through to the constructor.

class MyUIntValue(val v: UInt)

@JvmName("createMyUIntValue")
fun createMyUIntValue(v: UInt) = MyUIntValue(v)

According to the docs, this is the recommended way to make createMyUIntValue callable from Java code.

Sam
  • 8,330
  • 2
  • 26
  • 51