Skip to content

Add as to cast wrapped protocols to a concrete type#819

Merged
ktoso merged 7 commits into
swiftlang:mainfrom
madsodgaard:main
Jul 9, 2026
Merged

Add as to cast wrapped protocols to a concrete type#819
ktoso merged 7 commits into
swiftlang:mainfrom
madsodgaard:main

Conversation

@madsodgaard

Copy link
Copy Markdown
Contributor

Adds a new as method to wrapped interfaces, such that we can recover the concrete type again.

Tests and documentation were generated by Claude, but I validated it.

@madsodgaard madsodgaard requested a review from ktoso as a code owner July 7, 2026 11:07
Comment thread Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md Outdated
```java
<T extends JNISwiftInstance> Optional<T> as(Class<T> type, SwiftArena arena);
<T extends JNISwiftInstance> Optional<T> as(Class<T> type); // uses the default arena
```

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd prefer to not have signatures copy pasted into docs

Comment thread Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md Outdated
Comment thread Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md Outdated
Comment thread Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md Outdated
Comment thread Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md Outdated
Comment thread Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md Outdated
// so we use Java reflection to call the static native downcall instead.
// and also the "constructor" to get back the correct Java wrapper.
Method metadata = type.getDeclaredMethod("$typeMetadataAddressDowncall");
metadata.setAccessible(true);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We'll get warnings about this AFAIR on newer JDKs unless I'm misremembering, but that's fine for the time being... Better would be to just make the method public and avoid the setAccessible

Comment thread SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftDowncastable.java Outdated
Comment thread SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftDowncastable.java Outdated
Comment thread SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftDowncastable.java Outdated
Comment thread SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftDowncastable.java Outdated
Comment thread SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftDowncastable.java Outdated
@ktoso

ktoso commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

LGTM, just needed to calm down Claude's over-explaining in docs a bit... Please try to trim it down in the docs some more next time 🙏

@ktoso ktoso merged commit c235a0f into swiftlang:main Jul 9, 2026
42 checks passed
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