Skip to content

Adds support for optional mapping #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 28, 2024

Conversation

jrosen081
Copy link
Contributor

Closes #92

Adds support for methods and variables with optional. I went for an approach of making a wrapper func/var with the Optional suffix, but am open to other ideas. Also fixed up the Makefile so i could use it

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.

Some minor comments, looks like a good step


public func emptyOptional() -> Int64? {
Optional(javaOptional: empty())
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure I understand where the ofOptional and emptyOptional came from? I know this is generated but couldn't quite wrap my head around what the intent of these is?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a fun one:
Any method that returns a Java optional (or friends) now has a second method that returns a swift optional and does wrapping. A fun consequence of this is that all optionals have static methods to create themselves ... which then have a helper generated to make a swift optional. It's a fun bit of recursion-ish stuff

@JavaMethod
open func orElseThrow() -> JavaObject!
}
extension JavaClass {
Copy link
Collaborator

@ktoso ktoso Nov 27, 2024

Choose a reason for hiding this comment

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

Does this look right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks to be how we import static methods/functions for classes that have generic parameters. Seems to be the same way we do it for HashMap since the logic for the generic gets put into the function itself. Unless I misunderstood what you meant?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmmm, I see (note: I've not spend much time with JavaKit but more focused on the opposite direction, so thanks for helping review this 😄)...

Yeah that makes sense I guess, I wonder if this will become a problem in the long term with names clashing -- lots of types like to have a of(_:) or some form of create(_:) but maybe it'll work out okey, could be a lot of overloads eventually hm

@ktoso
Copy link
Collaborator

ktoso commented Nov 27, 2024

A bunch of compile errors discovered by the Sample, please check

@jrosen081
Copy link
Contributor Author

A bunch of compile errors discovered by the Sample, please check

Updated the code to get it running correctly with more samples!

@ktoso ktoso merged commit 2cf42ce into swiftlang:main Nov 28, 2024
12 checks passed
@ktoso
Copy link
Collaborator

ktoso commented Nov 28, 2024

Very nice, thanks again :-)

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.

Java2Swift: Consider translating java.util.Optional into Swift.Optional
2 participants