Re: [RFC] Enumerations, Round 2
| From: | Larry Garfield | Date: | Wed, 30 Dec 2020 20:21:42 +0000 |
| Subject: | Re: [RFC] Enumerations, Round 2 | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112684@lists.php.net to get a copy of this message | ||
On Wed, Dec 30, 2020, at 12:30 PM, Aleksander Machniak wrote:
> On 28.12.2020 21:21, Larry Garfield wrote:
> > https://wiki.php.net/rfc/enumerations
> Why can't this be simplified to:
>
> enum Size {
> case Small;
> case Medium;
> case Large;
> }
>
> 'Small' === Size::Small->value; // true
> Size::from('Small') === Size::Small; // true
>
> enum Suit {
> case Hearts = 'H';
> case Diamonds = 'D';
> case Clubs = 'C';
> case Spades = 'S';
> }
>
> 'H' === Suit::Hearts->value; // true
> 'Hearts' === Suit::Hearts->value; // false
That's a possibility we've been kicking around. I'm considering it, Ilija
doesn't care for it. :-) What's your argument in favor?
Related: There is an internal "case" value that is used as well. It could be exposed as a
normal read-only property, which would always be equal to the name of the case ("Hearts"),
regardless of whether another "value" is defined. We're still undecided on that,
too.
> Also, how about using a method instead of property for ->value?
We originally did. Ilija preferred moving it to a property since there will need to be one anyway
to store the value. I moderately favor a method but don't greatly care either way.
--Larry Garfield