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

A proposal for supporting references to synthetic Java properties #329

Merged
merged 10 commits into from
Dec 14, 2022
Prev Previous commit
Next Next commit
Polishing
  • Loading branch information
strangepleasures committed Dec 9, 2022
commit 2b4fb4dbbc0510e64d9a9e7c3b59844b4892404d
16 changes: 4 additions & 12 deletions proposals/references-to-java-synthetic-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ val widgetName = widget::name
widgetName.set("Widget1")
```
That functionality was never widely advertised, and was only available under a feature flag.
It also wasn't properly documented add covered with tests.
It also wasn't properly documented or covered with tests.
Nonetheless, it was enabled by default in `.gradle.kts` (see [KT-35933](https://youtrack.jetbrains.com/issue/KT-35933)).
Until recently, it didn't work in K2 (see [KT-54770](https://youtrack.jetbrains.com/issue/KT-54770)); the issue has been
solved in our prototype.
Expand Down Expand Up @@ -96,15 +96,8 @@ That solution has been implemented in our prototype.
If a significant number of users asks for full reflection, we'll reconsider this decision.

The fact that synthetic Java properties aren't included in `KClass.members` doesn't seem to be an issue.
Even though it wouldn't be difficult to include them, such a change could potentially break users' code.
Very often Java synthetic properties use an underlying field whose name is the same as the name of the property itself.
As we already include Java fields in `KClass.members`, adding also Java synthetic properties would mean that `KClass.members` would
contain two `KProperty` entries with the same type and name for each synthetic property.
User code using `KClass.members` to directly access Java fields may break after addition of extra members.
This consideration seems to be a serious argument against inclusion of synthetic Java properties into `KClass.members`.
```kotlin
val nameField = Widget::class.memberProperties.single { it.name == "name"} // would fail if we include both the field and the synthetic property
```
By design, `KClass.members` returns only real members and doesn't include any synthetic ones.
We could possibly consider introducing a separate property for them, e.g. `KClass.syntheticMemebers`.

## Reference resolution strategy

Expand Down Expand Up @@ -137,5 +130,4 @@ To sum up, we propose the following:
- Make it possible to reference synthetic Java properties using the `::` syntax.
- Disable `kotlin-reflect`-dependent features for now; throw an `UnsupportedOperationException` with a message making
it clear that the limitation applies only to Java synthetic properties.
- Do not include synthetic Java properties in `KClass.members`.
- When checking reference expressions for overload ambiguity th
- Do not include synthetic Java properties in `KClass.members`.