Skip to content

Expose Java-friendly instance methods and constructors for Swift types#9

Merged
DougGregor merged 1 commit into
swiftlang:mainfrom
DougGregor:java-instance-methods-and-ctors
Sep 29, 2024
Merged

Expose Java-friendly instance methods and constructors for Swift types#9
DougGregor merged 1 commit into
swiftlang:mainfrom
DougGregor:java-instance-methods-and-ctors

Conversation

@DougGregor
Copy link
Copy Markdown
Member

When projecting a Swift type into a Java class, map Swift methods into instance methods (rather than static methods) and Swift initializers into Java constructors (rather than static 'init' methods). This means our example Swift class like this:

public class MySwiftClass {
  public init(len: Int, cap: Int) { ... }
  public func voidMethod() { ... }
} 

turns into

public final class MySwiftClass {
  public MySwiftClass(long let, long cap) { /* downcall to Swift */ }
  public void voidMethod() { /* downcall to Swift */ }
}

as one would expect.

When projecting a Swift type into a Java class, map Swift methods into
instance methods (rather than static methods) and Swift initializers
into Java constructors (rather than static 'init' methods).
@lokesh-tr
Copy link
Copy Markdown
Contributor

lokesh-tr commented Sep 29, 2024

Also worth highlighting that this PR fixes an issue where running the make jextract-run produces java files with Canonical class names to represent the type instead of its simple name.

For example: at the current state make jextract-run will produce a java file with

public final class com.example.generated.MySwiftClass {
     // it also uses com.example.generated.MySwiftClass in function return types & parameters, etc. where MySwiftClass is supposed to be

instead of

public final class MySwiftClass {

People who try the Swift2Java route are very likely to hit this issue unless this PR gets merged.

@DougGregor DougGregor merged commit 87f701a into swiftlang:main Sep 29, 2024
@DougGregor DougGregor deleted the java-instance-methods-and-ctors branch September 29, 2024 20:11
@DougGregor
Copy link
Copy Markdown
Member Author

Thank you, Lokesh. I've gone ahead and merged it.

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