kotlin icon indicating copy to clipboard operation
kotlin copied to clipboard

RefinedForObjC and RefinedInSwift annotations

Open rickclephas opened this issue 2 years ago • 1 comments

Relates to KT-42297.

The Kotlin ObjC/Swift is pretty great, but at the moment there are also some gaps (especially with the Swift interop) like suspend functions and enum classes. With some boilerplate code the interop for such code can be improved, which can even be automated with a compiler plugin/annotation processor. However one limitation of such boilerplate code is that it adds unnecessary declarations to your public API.

With the RefinedForObjC and RefinedInSwift annotations we can hide these unnecessary declarations from the public API. We also have the meta-annotations RefinesForObjC and RefinesInSwift such that annotation processors can generate ObjC/Swift friendly APIs while automatically hiding the original declaration.

RefinedForObjC

Functions and properties annotated with the RefinedForObjC annotation won't be exported to ObjC. This allows you to create a more ObjC friendly version in your Kotlin code.

With the ObjCName annotation from #4815 you could even use the original name of the function/property for your ObjC friendly version.

RefinedInSwift

The RefinedInSwift annotation adds the swift_private attribute to the declaration. This results in the declarations being prefixed with __, which make them "invisible" from Swift. These declarations can still be used in your Swift code to create your Swift friendly API, but won't be shown in e.g. the Xcode autocomplete.

rickclephas avatar May 09 '22 20:05 rickclephas

Hi. Thank you for the PR! We will take a look later, hopefully next week.

SvyatoslavScherbina avatar May 13 '22 12:05 SvyatoslavScherbina

@rickclephas could you please run :compiler:tests-for-compiler-generator:generateTests Gradle task and push the resulting changes?

SvyatoslavScherbina avatar Aug 17 '22 12:08 SvyatoslavScherbina

@SvyatoslavScherbina done 👍🏻

rickclephas avatar Aug 17 '22 12:08 rickclephas

@rickclephas Thank you! From the side of FIR part everything is ok. If @SvyatoslavScherbina also thinks that PR is ready, then I will ask for one more thing: can you please cleanup commit history?

  • squash commits with fixes
  • add prefixes to commit messages to indicate which subsystem is modified ([FE 1.0] for old frontend, [FIR] for FIR frontend, [Native] for native backend, etc)

demiurg906 avatar Aug 19 '22 08:08 demiurg906

Cool! Updated the name and squashed the commits into three new ones.

rickclephas avatar Aug 19 '22 14:08 rickclephas

@demiurg906 would you like a PR for the FIR checkers (with similar improvements) for @ObjCName (#4815) as well?:

rickclephas avatar Aug 19 '22 14:08 rickclephas

@rickclephas Yes, it would be nice

demiurg906 avatar Aug 19 '22 14:08 demiurg906

Thank you!

SvyatoslavScherbina avatar Aug 22 '22 14:08 SvyatoslavScherbina