Skip to content

Class inheritance#230

Open
andyfriesen wants to merge 2 commits into
masterfrom
class-inheritance
Open

Class inheritance#230
andyfriesen wants to merge 2 commits into
masterfrom
class-inheritance

Conversation

@andyfriesen

@andyfriesen andyfriesen commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Rendered

See #229 for the main discussion thread on classes as a whole.

Also introduce the `open` keyword to allow classes to explicitly opt
into inheritance.
@andyfriesen andyfriesen mentioned this pull request Jul 16, 2026
@Bottersnike

Bottersnike commented Jul 16, 2026

Copy link
Copy Markdown

Why is field redefinition with subtypes disallowed but methods are allowed to be redefined with a subtype? nvm I can see why it would cause issues depending on where it's accessed from

@deviaze

deviaze commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

I like that inheritable classes must be prefixed by open. It's a great indicator of when a class is supposed to be inherited upon, makes inheritance opt in instead of a default behavior, and prevents library authors from inadvertently adding a prop to a class that wasn't meant to be inherited and breaking downstream users who extend it.

@TenebrisNoctua

Copy link
Copy Markdown

This looks pretty good, except that I think Java's step of using final is better than open. An opt-out is better IMO, where I can extend any class, except the ones the developer does not want, versus I cannot extend any class except the ones the developer specifically goes out of their way to allow for inheritance, which is a thought that doesn't occur most of the time, even when the developer isn't against inheritance of their classes.

That aside, the reasoning for super is quite strong especially within the presence of inheriting constructors. It should be re-considered.

Comment thread docs/class-inheritance.md

The original class RFC states that class names are hoisted and so can be used before the class declaration has been evaluated. This is unchanged by this RFC, but carries with it a consequence that is important to call out:

Class declarations have effects at the top level. Therefore, a class cannot inherit from another class that occurs lexically after it within the module.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Again, going to mention this. Implicit hoisting is a hack and doesn't even work for 90% of the cases it would be useful for. Get rid of it.

Comment thread docs/class-inheritance.md
## Summary

```luau
open class Animal

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Orthogonal to this RFC, but could we also have syntax for abstract methods? Using error("override me") as the implementation in the base class won't scale since that can error at runtime, but an abstract method can error during class prototyping.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's worth potentially also considering interfaces, and deciding on abstract vs interfaces vs both (both can be quite useful to have, but also potentially too much stuff going on depending on the desired design direction).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

an abstract keyword seems to be the best option personally.

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.

6 participants