Skip to content

Commit 292da83

Browse files
authored
update: clarify @JvmExposeBoxed behavior for inherited functions (#4947)
* update: clarify @JvmExposeBoxed behavior for inherited functions * fix: fixing header for inline value classes * update: implement TWr review comments * adding link to inheritance
1 parent 23fbb5f commit 292da83

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ fun emptyList(): List<Nothing> = listOf()
586586
// List emptyList() { ... }
587587
```
588588

589-
### Inline value classes
589+
## Inline value classes
590590

591591
<primary-label ref="experimental-general"/>
592592

@@ -639,3 +639,29 @@ MyInt output = ExampleKt.timesTwoBoxed(input);
639639

640640
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.
641641
Compiling with this option has the same effect as if every declaration in the module has the `@JvmExposeBoxed` annotation.
642+
643+
### Inherited functions
644+
645+
The `@JvmExposeBoxed` annotation doesn't automatically generate boxed representations for inherited functions.
646+
647+
To generate the necessary representation for an inherited function, override it in the implementing or extending class:
648+
649+
```kotlin
650+
interface IdTransformer {
651+
fun transformId(rawId: UInt): UInt = rawId
652+
}
653+
654+
// Doesn't generate a boxed representation for the transformId() function
655+
@OptIn(ExperimentalStdlibApi::class)
656+
@JvmExposeBoxed
657+
class LightweightTransformer : IdTransformer
658+
659+
// Generates a boxed representation for the transformId() function
660+
@OptIn(ExperimentalStdlibApi::class)
661+
@JvmExposeBoxed
662+
class DefaultTransformer : IdTransformer {
663+
override fun transformId(rawId: UInt): UInt = super.transformId(rawId)
664+
}
665+
```
666+
667+
To learn how inheritance works in Kotlin and how to call superclass implementations using the `super` keyword, see [Inheritance](inheritance.md#calling-the-superclass-implementation).

0 commit comments

Comments
 (0)