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

Kotlin doesn't support exporting inline classes to Objective-C/Swift, making Result 2.x a no-go for APIs consumed in iOS #110

Open
tiwoc opened this issue Sep 5, 2024 · 0 comments

Comments

@tiwoc
Copy link

tiwoc commented Sep 5, 2024

The Kotlin compiler doesn't support exporting inline classes to Objective-C/Swift (docs, Interopedia), which Result 2.x is built upon. Instead of representing the result type in the headers, the compiler will use id (Objective-C) which translates to Any on the Swift side, meaning that it's impossible to get the value or error encapsulated in the result on the Swift side.

We're currently using Result 1.x to return results from shared Kotlin code to the native client apps on all platforms. I'm not sure there's anything that can be done on the Result end (short of moving away from inline classes again), but I wanted to call out that it's impossible for teams using Result in the API of their multiplatform library to upgrade to 2.x at the moment. A warning in the readme that 2.x can't be used in APIs exported to iOS might be very helpful for teams considering whether to adopt the library for their multiplatform project or not.

I understand the reasons why using inline classes is superior for the happy path, so please know that I don't want to put any blame on you.

Example

Kotlin code (slightly edited sample code from the KMP wizard):

fun greet(): Result<String, Nothing> {
    return Ok("Hello, ${platform.name}!")
}

Generated Objective-C header file with Result 1.1.21:

- (SharedKotlin_resultResult<NSString *, SharedKotlinNothing *> *)greet __attribute__((swift_name("greet()")));

Generated Objective-C header file with Result 2.0.0:

- (id _Nullable)greet __attribute__((swift_name("greet()")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants