Does anyone know why sometimes K/N extensions comp...
# kotlin-native
d
Does anyone know why sometimes K/N extensions compile to Obj-C categories and sometimes they compile to "ExtensionsKt" classes? Examples in thread.
This extension:
Copy code
// In NSDateExtensions.kt
fun NSDate.toInstant() = Instant.fromEpochSeconds(timeIntervalSince1970.toLong(), 0)
requires me to use
NSDateExtensionsKt.toInstant(date)
in Swift:
Copy code
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("NSDateExtensionsKt")))
@interface SharedNSDateExtensionsKt : SharedBase
+ (SharedKotlinx_datetimeInstant *)toInstant:(NSDate *)receiver __attribute__((swift_name("toInstant(_:)")));
@end
However, this extension:
Copy code
// In ByteArrayExtensions.kt
fun ByteArray.md5base64(): String = toByteString().md5().base64()
let's me call the extension
byteArray.md5base64()
directly:
Copy code
@interface SharedKotlinByteArray (Extensions)
- (NSString *)md5base64 __attribute__((swift_name("md5base64()")));
@end
I would always prefer my extensions end up in an Objective-C
(Extensions)
category, so that I can use the extensions the same way in Swift and Kotlin. It's frustrating to run into a compiler error that the extension doesn't exist, and then have to look up the
shared.h
header only to find that I need to go through the "ExtensionsKt" extension class helper. This is a small thing, not really important.. but I'm curious if there's something I can do to help force the
(Extensions)
category for K/N code gen.
I looked at the bugs related to this trying to find some documentation or explanation - https://youtrack.jetbrains.com/issues/KT?q=Extension%20category - but didn't see anything that jumped out at me.
r
The difference in your examples is that
NSDate
is an ObjC type and
ByteArray
is a regular Kotlin type.
d
Ah, thanks for the link and the response. I glossed right over that...
👍 1