Skip to content

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

Merged
merged 9 commits into from
Mar 13, 2021
Merged

Syntactic support for PHP enums #23

merged 9 commits into from
Mar 13, 2021

Conversation

thekid
Copy link
Member

@thekid thekid commented Mar 8, 2021

See xp-framework/compiler#100

Unit enums

enum Suit {
  case Hearts;
  case Diamonds;
  case Clubs;
  case Spades;
}
  • Supported

Backed enums

enum Suit: string {
  case Hearts = 'H';
  case Diamonds = 'D';
  case Clubs = 'C';
  case Spades = 'S';
}
  • Supported

Grouped syntax

enum Direction {
  case Left, Right;
}

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

  • Supported

thekid added 3 commits March 7, 2021 22:28
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);
Copy link
Member Author

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.

Copy link
Member Author

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.

thekid added a commit to xp-framework/compiler that referenced this pull request Mar 10, 2021
thekid added 3 commits March 12, 2021 20:06
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
@thekid thekid merged commit 5916636 into master Mar 13, 2021
@thekid thekid deleted the feature/php-enums branch March 13, 2021 15:43
@thekid
Copy link
Member Author

thekid commented Mar 13, 2021

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.

1 participant