Skip to content

Make Instant.fromEpochMilliseconds available from iOS #78

Open
@lammertw

Description

@lammertw

Currently using KMM to iOS there is no constructor for Instant. So creating an instant from epoch milliseconds is impossible from iOS unless creating an additional helper function for this purpose in Kotlin.

Activity

dkhalanskyjb

dkhalanskyjb commented on Dec 4, 2020

@dkhalanskyjb
Collaborator

This is odd. Our tests include code like Instant.fromEpochMilliseconds(Clock.System.now().toEpochMilliseconds()), and they build and run on iOS simulator. Could you please provide of a problematic code snippet and the corresponding error?

lammertw

lammertw commented on Dec 4, 2020

@lammertw
Author

But are those tests written in Swift or Objective-c or in Kotlin. In Kotlin it works. It's just that it's not accessible from Swift or Objective-c. Here is the generated obj-c header of Instant:

__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("Kotlinx_datetimeInstant")))
@interface CommonKotlinx_datetimeInstant : CommonBase <CommonKotlinComparable>
- (int32_t)compareToOther:(CommonKotlinx_datetimeInstant *)other __attribute__((swift_name("compareTo(other:)")));
- (BOOL)isEqual:(id _Nullable)other __attribute__((swift_name("isEqual(_:)")));
- (NSUInteger)hash __attribute__((swift_name("hash()")));
- (CommonKotlinx_datetimeInstant *)minusDuration:(double)duration __attribute__((swift_name("minus(duration:)")));
- (double)minusOther:(CommonKotlinx_datetimeInstant *)other __attribute__((swift_name("minus(other:)")));
- (CommonKotlinx_datetimeInstant *)plusDuration:(double)duration __attribute__((swift_name("plus(duration:)")));
- (int64_t)toEpochMilliseconds __attribute__((swift_name("toEpochMilliseconds()")));
- (NSString *)description __attribute__((swift_name("description()")));
@property (readonly) int64_t epochSeconds __attribute__((swift_name("epochSeconds")));
@property (readonly) int32_t nanosecondsOfSecond __attribute__((swift_name("nanosecondsOfSecond")));
@end;

@interface CommonKotlinx_datetimeInstant (Extensions)
@property (readonly) CommonKotlinx_datetimeLocalDate *localDate __attribute__((swift_name("localDate")));
@end;
lammertw

lammertw commented on Dec 4, 2020

@lammertw
Author

As you can see none of the companion functions are there.

dkhalanskyjb

dkhalanskyjb commented on Dec 8, 2020

@dkhalanskyjb
Collaborator

Ah, I see.

It should be noted that this can't be changed in kotlinx-datetime. The issue is with the tooling that generates an Apple framework: when you use a particular class in code that is exported to a framework, only its methods are included in the generated header. Companion objects (and, it seems, some extension methods) are not exported.

There are several possible solutions for this:

fun Instant.Companion.dummy(): Nothing = TODO()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cookbookUseful snippetsusageBuild problems in client projects

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @lammertw@dkhalanskyjb

        Issue actions

          Make Instant.fromEpochMilliseconds available from iOS · Issue #78 · Kotlin/kotlinx-datetime