-
Notifications
You must be signed in to change notification settings - Fork 0
Syntactic support for PHP enums #23
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
Conversation
Enums are by design a closed list, which inheritance would violate. Interfaces are allowed, but not parent classes.
|
||
$decl= new EnumDeclaration([], $name, $implements, [], [], $comment, $token->line); | ||
$parse->expecting('{', 'enum'); | ||
$decl->body= $this->typeBody($parse, $decl->name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not completely correct, as typeBody()
allows properties, which PHP enums do not. The RFC states this:
Specifically, the following features of objects are not allowed on enumerations: [...] Enum/Case properties - Properties are a form of state, and enum cases are stateless singletons. Metadata about an enum or case can always be exposed via methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll leave this as-is, the compiler may chose to raise errors about this; or we simply leave it to the PHP runtime.
https://wiki.php.net/rfc/enumerations#grouped_syntax states that this "may cause syntactic issues with the planned addition of tagged unions", which are defined in https://wiki.php.net/rfc/tagged_unions and may be one of the following: case None { } case Some(private mixed $value) { ... } ...in addition to the existing declarations: case ASC; case DESC = "desc"; In none of these case would a comma be ambiguous, therefore I think we might as well have it, thus allowing more concise syntax for enum declarations
See xp-framework/compiler#100
Unit enums
Backed enums
Grouped syntax
https://wiki.php.net/rfc/enumerations#grouped_syntax states that this "may cause syntactic issues with the planned addition of tagged unions", which are defined in https://wiki.php.net/rfc/tagged_unions and may be one of the following:
case None { }
case Some(private mixed $value) { ... }
...in addition to the existing declarations:
case ASC
case DESC = 'desc'
In none of these cases would a comma be ambiguous, therefore I think we might as well have it, thus allowing more concise syntax for enum declarations