Skip to content

Conversation

@madsodgaard
Copy link
Contributor

Adds support to JNI mode for extracting Foundation.Date as java.time.Instant.

Copy link
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat :-) Please add info about it to the feature list docs

| Existential parameters `f(x: any (A & B)) ` |||
| Existential return types `f() -> any Collection ` |||
| Foundation Data and DataProtocol: `f(x: any DataProtocol) -> Data` |||
| Foundation Date: `f(date: Date) -> Date` |||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it's an unusual conversion I think we have to add actual docs about it

@ktoso
Copy link
Collaborator

ktoso commented Jan 9, 2026

I keep thinking about this from an overall project perspective, it is a bit random to bless a few types, but yes this one would work kinda well...

@madsodgaard
Copy link
Contributor Author

madsodgaard commented Jan 9, 2026

@ktoso Hm, imo this project exists to provide a seamless Swift/Java interop story. Since we are generating Java code meant to be used on Java platforms, perhaps also by Java developers, I think it makes sense to bridge any matching types, such as UUID, Date,URL etc...

The alternative is that the developer has to use a generated type Swift.Date like we do for Data in FFM. That will probably often (for these kind of "utility" types") result in the developer having to convert their Java types to the Swift wrapped types. I understand why it makes sense for Data because of memory copying etc, but not sure that its the same motivation for the above types.

@ktoso ktoso self-requested a review January 14, 2026 05:48
@ktoso
Copy link
Collaborator

ktoso commented Jan 14, 2026

Hm, imo this project exists to provide a seamless Swift/Java interop story.

Primarily it must be: correct, safe, and then seamless. We must not sacrifice correctness just because it's convenient. The conversion is lossy, and could result in bugs in practice, e.g. if you're trying to key anything off the date.

Such echo function showcases how this is a lossy conversion:

import Foundation

public func echoDate(_ date: Date) -> Date {
  return date
}

and in Java + jextracted Date-as-Instant:

        Instant now = Instant.now();
        Instant back = MySwiftLibrary.echoDate(now);

        if (!now.equals(back)) {
            System.out.println("NOT EQUAL! " + now + " != " + back);
        } else {
            System.out.println("EQUAL");
        }

gives:

NOT EQUAL! 2026-01-14T05:45:55.764682Z != 2026-01-14T05:45:55.764682054Z
DONE.

That's not something we should be just doing by default.

Instead I propose:

  • import Foundation.Date as a wrapped type, same as we do Data
  • add a method on it called Instant toJavaInstant() which performs the conversion so people don't have to do it manually.

Sounds good?

Copy link
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah after all let's not do the automatic conversion because it could be a source of equality bugs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants