Re: Request for RFC Karma - Enum value semantics proposal
| From: | Larry Garfield | Date: | Sun, 30 Apr 2023 15:59:04 +0000 |
| Subject: | Re: Request for RFC Karma - Enum value semantics proposal | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-120165@lists.php.net to get a copy of this message | ||
On Fri, Apr 28, 2023, at 4:47 PM, Garet Claborn wrote:
> You are correct, thank you.
>
> The RFC draft has been posted to
> https://wiki.php.net/rfc/treat_enum_instances_as_values
>
> -Garet
I have already explained at length on the PR thread why this is a fundamentally wrong way to
approach Enums. We absolutely should work to improve the Enum collection story in PHP, but silently
casting Enums to scalars is the wrong way to do it.
As noted in the original Enum RFC:
> As objects, Enum cases cannot be used as keys in an array. However, they can be used as keys
> in a SplObjectStorage or WeakMap. Because they are singletons they never get garbage collected, and
> thus will never be removed from a WeakMap, making these two storage mechanisms effectively
> equivalent.
WeakMap may not be not ideal, but it is more ideal and effective than introducing silent
Enum->primitive casting, which we have explicitly and deliberately avoided until now. It can
still be used in mostly the same way as arrays are today; the only caveat would be if you're
expecting to use a mix of different types as keys, in which case the answer is to fix the code
instead because that's already a broken approach.
--Larry Garfield