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

Make Enum an abstract struct #15274

Merged

Conversation

straight-shoota
Copy link
Member

The Enum is not intended to be instantiated and the compiler won't even let us. This type just serves as a common ancestor for all enum types.

That fits exactly the defintion of abstract types, but for some reason this keyword is missing on Enum. Instead all protections against instantiations are implemented explicitly in the compiler (and they go even further than for normal abstract types).
Adding the abstract modifier to the type def is still a useful addition, at least for documentation purposes.
This shouldn't change any behaviour as it's already impossible to instantiate Enum.

Copy link
Contributor

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

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

I guess the Enum type was implemented before abstract keyword?

Could that lead to eventually simplify the compiler?

@straight-shoota
Copy link
Member Author

straight-shoota commented Dec 13, 2024

Could that lead to eventually simplify the compiler?

Probably not. Most of the restrictions are applied to several types (via Type#can_be_stored?), Enum is just one of them.

And the few restrictions that are specific to Enum and do not overlap with abstract (e.g. that you cannot inherit from it).

@straight-shoota straight-shoota added this to the 1.15.0 milestone Dec 13, 2024
@HertzDevil
Copy link
Contributor

This is where the compiler makes Enum abstract:

types["Enum"] = enum_t = @enum = NonGenericClassType.new self, self, "Enum", value
abstract_value_type(enum_t)

What happens here is that Crystal allows you to reopen an abstract type without the abstract keyword.

@straight-shoota straight-shoota merged commit 2f3e07d into crystal-lang:master Dec 16, 2024
69 checks passed
@straight-shoota straight-shoota deleted the chore/enum-abstract branch December 16, 2024 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants