https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
d

Dmitry Stakhov

03/21/2024, 12:41 PM
Hi everyone, I have a question related to KMP-NativeCoroutines library. cc @Rick Clephas I have a common code similar to this:
Copy code
// A.kt
interface AClass : BClass
interface BClass {
    @NativeCoroutines
    suspend fun foo()
}

class ImplClass : AClass {
    override suspend fun foo() {
        // do some work
    }
}
I am currently using the version
0.13.2
, the generated ObjC code looks like this:
Copy code
__attribute__((swift_name("BClass")))
@protocol ModulenameBClass
@required

/**
 * @note This method converts instances of CancellationException to errors.
 * Other uncaught Kotlin exceptions are fatal.
*/
- (void)fooWithCompletionHandler:(void (^)(NSError * _Nullable))completionHandler __attribute__((swift_name("foo(completionHandler:)")));
- (ModulenameKotlinUnit *(^(^)(ModulenameKotlinUnit *(^)(ModulenameKotlinUnit *, ModulenameKotlinUnit *), ModulenameKotlinUnit *(^)(NSError *, ModulenameKotlinUnit *)))(void))fooNative __attribute__((swift_name("fooNative()")));
@end
The
fooNative
function is generated as the
BClass
member here. I am planning to update the library version to the latest
1.0.0-ALPHA26
, but I see the generated ObjC code looks like this now:
Copy code
__attribute__((swift_name("BClass")))
@protocol ModulenameBClass
@required
@end

__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("ANativeKt")))
@interface ModulenameANativeKt : AppPlatformBase
+ (ModulenameKotlinUnit *(^(^)(ModulenameKotlinUnit *(^)(ModulenameKotlinUnit *, ModulenameKotlinUnit *), ModulenameKotlinUnit *(^)(NSError *, ModulenameKotlinUnit *), ModulenameKotlinUnit *(^)(NSError *, ModulenameKotlinUnit *)))(void))foo:(id<ModulenameBClass>)receiver __attribute__((swift_name("foo(_:)")));
@end
The
fooNative
function is generated as the
ANativeKt
class static function with one parameter
receiver
. What is the best ways to handle this? Can I update to the latest library version keeping the function as the class member it's essentially added in Kotlin code?
r

Rick Clephas

03/21/2024, 12:44 PM
Hi! You are experiencing a limitation in the Kotlin-ObjC interop regarding interfaces and extensions. To make the usage of such functions easier you can create a Swift extension. https://github.com/rickclephas/KMP-NativeCoroutines?tab=readme-ov-file#interface-limitations
thank you color 1
d

Dmitry Stakhov

03/21/2024, 1:01 PM
Looks like I need to make too many swift extensions, so there is no sense for migration 😅
I see after updating to Kotlin 2.0 the pre-alpha versions stop generating the
fooNative
function so writing the extensions can become necessary (or use with the static function as is) to make the project compatible with Kotlin 2.0
r

Rick Clephas

03/21/2024, 1:18 PM
Note: the pre-alpha versions only support up to Kotlin 1.7
d

Dmitry Stakhov

03/21/2024, 2:06 PM
Hmm, we are using 1.9.23 without issues. Does it mean it can have some unexpected behavior, because it's "not supported"?
r

Rick Clephas

03/21/2024, 2:34 PM
Yeah the compiler plugin API isn’t a stable part of Kotlin, so there aren’t any guarantees about Kotlin version compatibility.
2 Views