Skip to content

jextract/jni: Java overloads for trailing default arguments - #976

Open
piercifani wants to merge 1 commit into
swiftlang:mainfrom
piercifani:jni-default-arguments
Open

piercifani wants to merge 1 commit into
swiftlang:mainfrom
piercifani:jni-default-arguments

Conversation

@piercifani

@piercifani piercifani commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #424.

Why

Swift APIs rely heavily on default arguments, but Java has no equivalent. Java and Kotlin callers currently have to pass every argument, even when Swift provides a default. That means copying those defaults by hand. A view model’s callbacks struct, for example, may default all its closures to no-ops, yet Java callers still have to supply every closure.

What

In JNI mode, the generator now adds overloads that let callers omit trailing defaulted parameters. Given this Swift function:

public func greeting(name: String = "world", excited: Bool = false) -> String
MySwiftLibrary.greeting();             // "Hello, world."
MySwiftLibrary.greeting("Ana");        // "Hello, Ana."
MySwiftLibrary.greeting("Ana", true);  // "Hello, Ana!"

The generator skips an overload when:

  • A declared function already has the same name and Java parameter types. The declared function takes precedence.
  • The Swift call would be ambiguous. For example, another initializer might also accept no arguments, even if that initializer isn’t exported.
  • The overload could accept a SwiftArena and the next omitted parameter is a closure. Kotlin lambdas can convert to both types, making calls such as init(onOpen, SwiftArena) and init(onOpen, onClose) ambiguous.

Protocol requirements are unchanged because they cannot declare defaults. Callers must also continue to pass any defaulted parameter that isn’t trailing.

Two supporting changes allow overloads with closure parameters to compile. The overloads now share their closure parameters’ @javainterface wrappers and Java helper class, so each is emitted only once. The helper class also gets a private constructor, preventing Kotlin from treating Type.method() as either a no-argument method call or construction of the helper class Type.method.

Tests

  • JNIDefaultArgumentsTests: generated Java and Swift for the overloads, non-trailing defaults, declared overloads winning, ambiguous calls skipped, closure parameters and the arena case.
  • JNIClosureTests / JNIEscapingClosureTests: updated for the private constructor.
  • JNI sample: DefaultArgumentsTest calls each overload and checks that Swift's defaults are used.
  • Docs: a "Default arguments" section in FeaturesJextract.

swift test passes. FFM mode is unchanged.

Java and Kotlin callers had to pass every argument, including those with
Swift defaults (swiftlang#424).

Each function and initializer now gets an overload per trailing defaulted
parameter it can be called without, like the existing variadic expansion.
The overload's Swift thunk calls the function without those arguments, so
Swift supplies its own defaults; no default expression is translated.

An overload is left out when a declared function already has that name and
parameter types, when calling the Swift function with only those arguments
would be ambiguous, or when its result may take a SwiftArena and the next
parameter is a closure (ambiguous for Kotlin lambdas).

Overloads share their closure parameters' wrappers and Java helper class,
which are now printed once; the helper class gets a private constructor so
Kotlin's `Type.method()` isn't ambiguous with constructing it.

Fixes swiftlang#424

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@piercifani
piercifani requested a review from ktoso as a code owner November 27, 2026 14:16
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.

[jextract] Add support for extracting functions with default parameters

1 participant