Repository navigation
jextract/jni: Java overloads for trailing default arguments - #976
Open
piercifani wants to merge 1 commit into
Open
piercifani wants to merge 1 commit into
piercifani wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
The generator skips an overload when:
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.DefaultArgumentsTestcalls each overload and checks that Swift's defaults are used.swift testpasses. FFM mode is unchanged.