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

Support failable Initializers for Opaque Types #235

Open
NiwakaDev opened this issue Jul 7, 2023 · 5 comments
Open

Support failable Initializers for Opaque Types #235

NiwakaDev opened this issue Jul 7, 2023 · 5 comments

Comments

@NiwakaDev
Copy link
Collaborator

NiwakaDev commented Jul 7, 2023

Something like:

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        type SomeType;
        #[swift_bridge(init)]
        fn new() -> Result<SomeType, String>;
    }
}
do {
   let someType = try SomeType()
} catch let error {
   //..
}

Or

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        type SomeType;
        #[swift_bridge(init)]
        fn new() -> Option<SomeType>;
    }
}
if let someType = SomeType() {
    //...
}

I'd like to use this feature, but I don't know whether or not other users would like to.

@chinedufn
Copy link
Owner

I hadn't heard of this feature. Yeah this sounds like something that we want to support.

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization/#Failable-Initializers

I'm thinking we should also add a #[swift_bridge(init)] section to the Function Attributes documentation and have that section's bridge module show both regular and failable initializers

@nafehshoaib
Copy link

Any updates on this? Would love to use failable initializers instead of a static methods that return optionals

@NiwakaDev
Copy link
Collaborator Author

@nafehshoaib

Any updates on this?

Not yet.

Would love to use failable initializers instead of a static methods that return optionals

I guess that I can implement this issue in three weeks if needed.

@NiwakaDev
Copy link
Collaborator Author

@chinedufn
Can I implement this feature?

@chinedufn
Copy link
Owner

Yeah that would be great.

I think "Failable Initializers" are ones that return an Option.
We can refer to inits that can throw as "Throwing Initializers".

Feel free to implement either or both.

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

No branches or pull requests

3 participants