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

Simpler class instantiation #440

Closed
madsmtm opened this issue Apr 21, 2023 · 2 comments
Closed

Simpler class instantiation #440

madsmtm opened this issue Apr 21, 2023 · 2 comments
Labels
A-framework Affects the framework crates and the translator for them enhancement New feature or request help wanted Extra attention is needed

Comments

@madsmtm
Copy link
Owner

madsmtm commented Apr 21, 2023

Swift decided to combine allocation and initialization, such that you can simply call NSView.init() instead of Objective-C's cumbersome [[NSView alloc] init]. Actually, NSView.alloc() is entirely impossible!

For us, this means:

  • NSView::initWithFrame(mtm, frame) (or (frame, mtm), depending on what we decide in The deal with mutability #265) instead of NSView::initWithFrame(mtm.alloc(), frame).
  • NSString::initWithString(string) instead of NSString::initWithString(NSString::alloc(), string).

Should we do this? What would be the downsides? And how should this tie into our naming scheme in #284, since Rust prefers the name new?

Relates a bit to #438, since we would like the syntax for declaring initializers to resemble the syntax actually used when calling them.

@madsmtm madsmtm added enhancement New feature or request help wanted Extra attention is needed A-framework Affects the framework crates and the translator for them labels Apr 21, 2023
@madsmtm madsmtm changed the title Simpler initialization Simpler class instantiation Apr 21, 2023
@madsmtm madsmtm added this to the icrate v0.2.0 milestone May 26, 2023
@madsmtm
Copy link
Owner Author

madsmtm commented May 28, 2023

One downside would be that all superclasses would have to manually create initializer methods inherited on superclasses instead of e.g. doing Id::cast::<MySubclass>(MySuperclass::init(MySubclass::alloc().into_super())).

Though that should arguably be discouraged anyhow, and since we already generate the initializer methods from superclasses on subclasses in header-translator, this is more of a problem for downstream users.

@madsmtm
Copy link
Owner Author

madsmtm commented Apr 16, 2024

I'm not going to go down this route, as I think what the user declares in declare_class! should match what's in extern_methods!, and making declared initializers not take this: Allocated<Self> is going to feel inconsistent and weird.

Things will get better here anyhow after rust-lang/rfcs#3519, which will allow us to write self: Allocated<Self>, and call with NSView::alloc_on_main(mtm).initWithFrame(frame) / NSString::alloc().initWithString(string).

@madsmtm madsmtm closed this as not planned Won't fix, can't repro, duplicate, stale Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-framework Affects the framework crates and the translator for them enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant