Request for RFC Karma - Enum value semantics proposal

php.internals

Garet Claborn

3 years ago
Relating to the existing thread in my introduction: Re: [PHP-DEV] Introduction - SuitespaceRnD I am requesting RFC karma to post the present version of an RFC made in response to the GitHub issue found at https://github.com/php/php-src/issues/9208 Though any guidance or contributions from those more familiar with this portion of the codebase is more than welcome; we are happy to assign work implementing this from our end. We would like to gauge interest, feedback and any brainstorming prior to beginning work in earnest. Thanks, Garet

Tim Düsterhus

3 years ago
Hi On 4/25/23 14:54, Garet Claborn wrote:
> I am requesting RFC karma to post the present version of an RFC made in > response to the GitHub issue found at > https://github.com/php/php-src/issues/9208
You should have already been granted RFC karma yesterday by derick, he told me that the Wiki would send a notification. Can you check whether you are already able to create the RFC page and if or if not you received an automated email about the permission adjustment? Best regards Tim Düsterhus

Garet Claborn

3 years ago
You are correct, thank you. The RFC draft has been posted to https://wiki.php.net/rfc/treat_enum_instances_as_values -Garet On Tue, Apr 25, 2023 at 8:14 AM Tim Düsterhus <tim@bastelstu.be> wrote:

Lokrain

3 years ago
Hello everyone, Would this change affect WeakMap somehow? Regards, Dimitar On Sat, 29 Apr 2023 at 0:48, Garet Claborn <garet@suiteux.com> wrote:

Lynn

3 years ago
On Fri, Apr 28, 2023 at 11:48 PM Garet Claborn <garet@suiteux.com> 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 think this example should be "mixed" instead of "$mixed"? `public function offsetGet($mixed $which){` Maybe a bit of a side-track, what about an interface with a method that lets an object specify what it would turn into if it were used as an array key? In the case of enums that would mean they'd implement this interface and just return the value. I hadn't seen this idea come by yet and I don't know if it's a good idea, just something I was thinking of while trying to group data objects based on a value. In theory this could also be done with Stringable instead. https://3v4l.org/Vi8kY#v8.2.5 ```php <?php interface ArrayKeyable { public function toArrayKey(): string|int; } final class MyObject implements ArrayKeyable { public function __construct( public readonly int $id, public readonly string $name, ) {} public function toArrayKey(): string { return $this->name; } } $objects = []; $data = [[1, 'Jane'], [2, 'John'], [3, 'Jake'], [4, 'Jane']]; foreach($data as [$id, $name]) { $object = new MyObject($id, $name); $objects[$object->toArrayKey()][] = $object; // would turn into $objects[$object][] = $object; } var_dump($objects); ```

Larry Garfield

3 years ago
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