Skip to content

Commit 38f16d7

Browse files
committed
further review
1 parent 8581aa5 commit 38f16d7

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

docs/topics/jvm/java-to-kotlin-interop.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,36 +591,47 @@ fun emptyList(): List<Nothing> = listOf()
591591
<primary-label ref="experimental-general"/>
592592

593593
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:
595595

596596
```kotlin
597-
@JvmInline value class PositiveInt(val number: Int)
597+
@JvmInline
598+
value class MyInt(val value: Int)
599+
```
600+
601+
So the following Java code fails:
602+
603+
```java
604+
MyInt input = new MyInt(5);
598605
```
599606

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:
601609

602610
* Class
603611
* Constructor
604612
* Function
605613

614+
Before using the `@JvmExposeBoxed` annotation in your code, you must opt in by using: `@OptIn(ExperimentalStdlibApi::class)`.
606615
For example:
607616

608617
```kotlin
618+
@OptIn(ExperimentalStdlibApi::class)
609619
@JvmExposeBoxed
610620
@JvmInline
611621
value class MyInt(val value: Int)
612622

623+
@OptIn(ExperimentalStdlibApi::class)
613624
@JvmExposeBoxed
614625
fun MyInt.timesTwoBoxed(): MyInt = MyInt(this.value * 2)
615626
```
616627

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:
619630

620631
```java
621632
MyInt input = new MyInt(5);
622633
MyInt output = ExampleKt.timesTwoBoxed(input);
623634
```
624635

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.
626637
Compiling with this option has the same effect as if every declaration in the module has the `@JvmExposeBoxed` annotation.

0 commit comments

Comments
 (0)