Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift classes can inherit from traits #2363

Merged
merged 1 commit into from
Dec 24, 2024

Conversation

mhammond
Copy link
Member

Builds on #2196, #2204 and #2297. Closes #2169.

@mhammond mhammond requested a review from bendk December 22, 2024 23:13
@mhammond mhammond requested a review from a team as a code owner December 22, 2024 23:13
Copy link
Contributor

@bendk bendk left a comment

Choose a reason for hiding this comment

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

Looks great to me.

I had a comment about the protocol name, but that's not really in-scope for this issue. If you agree and wanted to make that change too, that's good with me. If you don't agree or want to do it in a separate PR that's good too.

if has_callback_interface {
Ok(class_name)
} else {
Ok(format!("{class_name}Protocol"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a good reason why this has "Protocol" appended? IIUC, this is a Rust trait without callback interface support, so Swift can't implement it. In that case, it seems like protocol should get {class_name} which feels like a nicer name, and the class that implements the protocol should get something like{class_name}Impl. I think that's how the other languages handle it.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is exactly the same as kotlin which has "Interface" appended

Note that this change doesn't change the hierarchy of any existing objects, it just controls which of the 2 generated classes is used in the inheritance chain.

I'm inclined to agree that the existing hierarchy is odd in these cases, I fear that trying to change that here will accidentally introduce new regressions.

Is this also your understanding - ie, that this patch doesn't change the class hierarchy for existing types, and that doing so is something we'd need to be very careful about?

Copy link
Contributor

@bendk bendk Dec 23, 2024

Choose a reason for hiding this comment

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

Is this also your understanding - ie, that this patch doesn't change the class hierarchy for existing types

Yes agreed.

..and that doing so is something we'd need to be very careful about?

Maybe I have a slight difference of opinion here. I think it could be okay to make the change, since most end-users aren't going to see a difference. If they store a Foo and only use it to call methods from the protocol, it shouldn't matter if Foo is a concrete type that implements the protocol or the protocol itself.

I agree we should be careful, but maybe I'd remove the "very" part.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I think I got confused and believed that we had already made the change for other languages. If Kotlin works this way, then I think Swift should too. If we decide to change them, let's change both at the same time.

@mhammond
Copy link
Member Author

mhammond commented Dec 24, 2024

Also, I think I got confused and believed that we had already made the change for other languages. If Kotlin works this way, then I think Swift should too. If we decide to change them, let's change both at the same time.

I remember looking into this when doing this for kotlin - the names smelt wrong, but I couldn't work out how to make them good.

When UniFFI generates a plain-old trait, it only emits a protocol - there's no concrete implementation. So our NodeTrait generates public protocol NodeTrait : AnyObject {}.

But when we need a local impl of the trait (callback/foreign) we'd generate public protocol NodeTraitProtocol : AnyObject {} and open class NodeTrait which holds the pointer).

I don't see how to change those names?

@mhammond mhammond merged commit 8185a28 into mozilla:main Dec 24, 2024
5 checks passed
@mhammond mhammond deleted the push-vknmwmozssoy branch December 24, 2024 01:49
@praveenperera
Copy link
Contributor

awesome thanks @mhammond !

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.

Structs which #[uniffi::export] the impl of a trait should have that reflected in the bindings.
3 participants