Re: [RFC] Enumerations, Round 2
| From: | Rowan Tommins | Date: | Tue, 05 Jan 2021 13:10:58 +0000 |
| Subject: | Re: [RFC] Enumerations, Round 2 | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112761@lists.php.net to get a copy of this message | ||
On 04/01/2021 22:04, Mike Schinkel wrote:
I don't quit get how you are thinking of with ": string"; can you give an example?The current RFC requires the declaration of a Scalar Enum [1] to include the scalar type, so it looks like this: enum BookingStatus: string {
case PENDING = "PENDING"; case CONFIRMED = "CONFIRMED"; case CANCELLED = "CANCELLED";} The simplest opt-in for default scalar values would be to declare the type but no values, so the above could be abbreviated this way: enum BookingStatus: string {
case PENDING; case CONFIRMED; case CANCELLED;} However, it's probably better to be a bit more explicit, e.g.: enum BookingStatus: auto string {
case PENDING; case CONFIRMED; case CANCELLED;} Either way, we'd have to decide if it was allowed to specify some values but not all, e.g.: enum BookingStatus: auto string {
case PENDING; case CONFIRMED; case CANCELLED = 'TERMINATED';} A more verbose but possibly clearer syntax would be a token on the case statements themselves, such as "= auto", to mean "set the value for this case based on its constant name": enum BookingStatus: string {
case PENDING = auto; case CONFIRMED = auto; case CANCELLED = 'TERMINATED';} There's also the question of whether an automatic value for ints should also be allowed, e.g. incrementing in declaration order. This is more dangerous, as adding, removing, or re-arranging cases can affect the values of *all* the cases in the enum, but is done in some other languages. [1] https://wiki.php.net/rfc/enumerations#scalar_enums Regards, -- Rowan Tommins [IMSoP]