You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/topics/jvm/java-to-kotlin-interop.md
+17-6Lines changed: 17 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -591,36 +591,47 @@ fun emptyList(): List<Nothing> = listOf()
591
591
<primary-labelref="experimental-general"/>
592
592
593
593
Kotlin compiles [inline value classes](inline-classes.md) to use **unboxed representations**, which are often inaccessible from Java.
594
-
For example, Java isn't able to call a constructor for the `PositiveInt` class:
594
+
For example, Java isn't able to call a constructor for the `MyInt` class:
595
595
596
596
```kotlin
597
-
@JvmInline value classPositiveInt(valnumber:Int)
597
+
@JvmInline
598
+
value classMyInt(valvalue:Int)
599
+
```
600
+
601
+
So the following Java code fails:
602
+
603
+
```java
604
+
MyInt input =newMyInt(5);
598
605
```
599
606
600
-
You can apply the `@JvmExposeBoxed` annotation at the following levels to ensure fine-grained control over what's exposed to Java:
607
+
You can use the [`@JvmExposeBoxed`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.jvm/-jvm-expose-boxed/) annotation so that Kotlin generates a public constructor that Java can call directly.
608
+
You can apply the annotation at the following levels to ensure fine-grained control over what's exposed to Java:
601
609
602
610
* Class
603
611
* Constructor
604
612
* Function
605
613
614
+
Before using the `@JvmExposeBoxed` annotation in your code, you must opt in by using: `@OptIn(ExperimentalStdlibApi::class)`.
606
615
For example:
607
616
608
617
```kotlin
618
+
@OptIn(ExperimentalStdlibApi::class)
609
619
@JvmExposeBoxed
610
620
@JvmInline
611
621
value classMyInt(valvalue:Int)
612
622
623
+
@OptIn(ExperimentalStdlibApi::class)
613
624
@JvmExposeBoxed
614
625
fun MyInt.timesTwoBoxed(): MyInt=MyInt(this.value *2)
615
626
```
616
627
617
-
With these annotations, Kotlin generates a Java-accessible constructor for the `MyInt` class **and**an overload for the
618
-
extension function that uses the boxed form of the value class. So you can write the following Java code:
628
+
With these annotations, Kotlin generates a Java-accessible constructor for the `MyInt` class **and**a variant for the
629
+
extension function that uses the boxed form of the value class. So the following Java code runs successfully:
619
630
620
631
```java
621
632
MyInt input =newMyInt(5);
622
633
MyInt output =ExampleKt.timesTwoBoxed(input);
623
634
```
624
635
625
-
To apply this behavior to all inline value classes within a module, compile it with the `-Xjvm-expose-boxed` option.
636
+
To apply this behavior to all inline value classes and the functions that use them within a module, compile it with the `-Xjvm-expose-boxed` option.
626
637
Compiling with this option has the same effect as if every declaration in the module has the `@JvmExposeBoxed` annotation.
0 commit comments