-
Notifications
You must be signed in to change notification settings - Fork 362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose boxed inline value classes in JVM #393
base: master
Are you sure you want to change the base?
Conversation
"Exposing" a `value` class is not a single concept. The following are some scenarios that we would like to support: | ||
|
||
1. Creating boxed values: for example, creating an actual instance of `PositiveInt`. | ||
2. Calling operations that take or return value classes using their boxed representations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides calling, there's also overriding, which the current proposal doesn't handle (or does it?), but perhaps it should be mentioned explicitly.
proposals/jvm-expose-boxed.md
Outdated
The current compilation scheme transforms every operation where a value class is involved into a static function that takes the unboxed variants, and whose name is mangled to prevent clashes. This means those operations are not available for Java consumers. We propose introducing a new `@JvmExposeBoxed` annotation that exposes a variant of the function taking and returning the boxed versions instead (if more than one argument or return type is a value class, the aforementioned variant uses the boxed versions for _all_ of them). We call it the _boxed variant_ of the function. | ||
|
||
```kotlin | ||
annotation class JvmExposedBoxed(val jvmName: String = "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to specify retention and targets explicitly here.
proposals/jvm-expose-boxed.md
Outdated
annotation class JvmExposedBoxed(val jvmName: String = "") | ||
``` | ||
|
||
The `@JvmExposeBoxed` annotation may be applied to a declaration, or a declaration container (classes, files). In the latter case, it should be taken as applied to every single declaration within it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it allowed on open members? If yes, it kind of makes KT-31420 partially supported which is a bit suspicious. In particular, we should probably examine all problems keeping us from implementing KT-31420 and see how they affect @JvmExposeBoxed
.
> There is a corner case in which `@JvmExposeBoxed` with a name is always needed: when the function has no argument which is a value class, but returns a value class. | ||
> In that case the compilation scheme performs no mangling. As a result, without the annotation the Java compiler would produce an ambiguity error while resolving the name. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not fully correct, mangling is still performed for class members.
CONSTRUCTOR, PROPERTY, FUNCTION, // callables | ||
CLASS, FILE, // containers | ||
) | ||
annotation class JvmExposedBoxed(val jvmName: String = "", val expose: Boolean = true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo - JvmExposedBoxed
instead of JvmExposeBoxed
.
Only comment here about the text itself. The discussion about the feature is done in #394.
We propose modifications to how value classes are exposed in JVM, with the goal of easier consumption from Java. This includes exposing the constructor publicly and giving the ability to expose boxed variants of operations.