Skip to content

Calling super methods safely #455

Open
@madsmtm

Description

@madsmtm

Calling a super method is (with the below restrictions) as safe as calling regular methods.

Since doing so is a common requirement, e.g. in initializers, or when subclassing e.g. NSView, we should find a way so that the user can easily do so.

Requirements that I know of so far:

  • Only safe when declaring a new subclass.
  • Only direct super methods (e.g. calling NSResponder's methods from a subclass of NSView could potentially lead to UB, if NSView was implemented in a certain way.
  • Super method calls can only access methods on the superclass, not on the declared class.
  • Initializer methods only from inside other initializer methods.

The main problem with this is that it effectively doubles the amount of methods that we have to generate, the codegen is different for each. Though maybe with #448, that will be less of a problem?

If we had inherent traits, or something like it, then it would perhaps be easier, since we could make the implementation on a trait instead.

Related: Internally, we can consider getting rid of send_message_super, by providing a struct Super<T: MessageReceiver> { obj: T, cls: &'static Class } that implements MessageReceiver::send_message by calling objc_msgSendSuper instead.

Related:

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-frameworkAffects the framework crates and the translator for themA-objc2Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` cratesenhancementNew feature or requestquestionFurther information is requested

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions