Skip to content

Conversation

@Clemo97
Copy link

@Clemo97 Clemo97 commented Jan 29, 2026

Fixes #526

Problem

Swift allows method overloading using parameter labels (argument labels), where methods can have the same base name but different parameter labels:

public func takeValue(a: String) {}
public func takeValue(b: String) {}

In Swift, these are distinct methods because parameter labels are part of the function signature. However, when translated to Java, both were being generated with the same signature, causing compilation failures:

public void takeValue(String) //   Compilation error: duplicate method

Error:

error: method takeValue(String) is already defined in class OverloadSample

Solution

Modified the Java method name generation in FFMSwift2JavaGenerator+JavaTranslation.swift to append parameter labels as suffixes, creating unique method names that preserve Swift semantics while ensuring valid Java code.

Implementation:

  • Parameter labels are joined with underscores (e.g., _a, _b)
  • Applied consistently to all methods (functions, initializers)
  • Getters and setters are excluded to maintain property access patterns

Example transformation:

// Swift
public func takeValue(a: String) {}
public func takeValue(b: String) {}
// Generated Java (Before)
public void takeValue(String a) //   Duplicate!
public void takeValue(String b) //   Compilation error

// Generated Java (After)
public void takeValue_a(String a) //   Unique
public void takeValue_b(String b) //   Compiles successfully

Changes

  • Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaTranslation.swift: Added logic to append parameter labels to method names for disambiguation
  • Samples/SwiftJavaExtractFFMSampleApp/src/main/java/com/example/swift/HelloJava2Swift.java: Updated sample code to use new method naming convention (e.g., globalTakeInt_i(), init_len_cap())
  • Samples/SwiftJavaExtractFFMSampleApp/Sources/MySwiftLibrary/OverloadSample.swift: Added test cases with overloaded methods.

@Clemo97 Clemo97 requested a review from ktoso as a code owner January 29, 2026 15:38
@ktoso
Copy link
Collaborator

ktoso commented Jan 29, 2026

Thanks for the request.what’s a reasonable direction however we should only be doing this if we detect a method has conflicts, otherwise it could be pretty annoying. Many members don’t have overloads and we can still call them with her normal names after all.

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.

Yeap still missing the required logic.

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.

marking as requesting changes

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.

Swift overloads produce uncompilable wrappers

2 participants