Re: [RFC] Enumerations
| From: | Larry Garfield | Date: | Mon, 07 Dec 2020 15:26:36 +0000 |
| Subject: | Re: [RFC] Enumerations | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112456@lists.php.net to get a copy of this message | ||
On Mon, Dec 7, 2020, at 2:46 AM, Michał Marcin Brzuchalski wrote:
> Hi Larry,
>
> Thanks for taking the topic. I love it.
>
> Regarding the
::cases() method on UnitEnum I guess it'd be
> more natural
> to cast enum into an array like:
>
> (array) Suit;
>
> but I realize it'd be harder to implement. The question is if it was even
> considered?
>
> Regarding the ::from() method responsible for casting was a
> natural cast
> operator considered instead?
> For eg.
> $suit = (Suit) $record['suit'];
> Instead of:
> $suit = Suit::from($record['suit']);
>
> Cheers,
> Michał Marcin Brzuchalski
We didn't really get into explicit casting, since it's so rarely used in conventional PHP
code these days. I'm not sure off hand if supporting (Suit)"H" or
(string)Suit::Hearts would be easy or hard. That would be an Ilija question.
Assuming it's feasible to do, what do people feel about supporting that? IMO, cases(), from(),
and values() need to be kept no matter what as they're more self documenting and can be passed
around as callables. So the question is just whether we should also try to add casting as an alias
to those operations.
--Larry Garfield