Skip to content
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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Conversation

serras
Copy link
Contributor

@serras serras commented Sep 12, 2024

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.

"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.
Copy link
Member

@udalov udalov Sep 12, 2024

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.

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 = "")
Copy link
Member

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.

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.
Copy link
Member

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.

Comment on lines +175 to +176
> 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.
Copy link
Member

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)
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants