I noticed recently that Kotlin extensions are no l...
# kotlin-native
d
I noticed recently that Kotlin extensions are no longer exposed to ObjC/Swift as extensions, but instead as classmethods on a somewhat unusually named class... I'm pretty sure that they actually appeared as extensions using an earlier build though. What is the reason for the change? Will be extensions be returning at some point? (Or am I just mistaken?)
s
Kotlin extensions to some types are exposed as extensions to ObjC/Swift, while extensions to other types aren’t:
Copy code
fun Any.extension() {}
->
Copy code
@interface Kotlin : KotlinBase
+ (void)extension:(id)receiver __attribute__((swift_name("extension(_:)")));
@end;
Copy code
class Foo
fun Foo.extension() {}
->
Copy code
@interface KotlinFoo (Extensions)
- (void)extension __attribute__((swift_name("extension()")));
@end;
Could you provide an example of extension which translation has been changed?
d
Hmm you are probably right and I'm misremembering. Why are extensions on Kotlin types not exposed as extensions?
s
Extensions to some Kotlin types aren't exposed as extensions due to limitations of Objective-C language or/and technical limitations and complexity.
d
@svyatoslav.scherbina Any chance you could elaborate on "some Kotlin types" (is this documented anywhere?) and/or what the limitations and additional complexities are? Partially for curiosity, but I would also like to know if there's anything I can do to make "extension-compatible" types.
s
Currently Kotlin/Native exposes only extensions to Kotlin classes that are not interfaces and are not collections/functions/strings etc. Objective-C doesn’t have any kind of extensions to protocols, and all extensions to classes (i.e. category methods) are added to virtual dispatch and thus can unexpectedly clash with other methods at runtime, so we avoid adding methods to
NSString
, Cocoa collections and other standard classes.
d
Thanks for the explanation, @svyatoslav.scherbina! That makes sense. simple smile