Re: ARRAY_UNIQUE_IDENTICAL option
| From: | someniatko | Date: | Thu, 03 Nov 2022 18:53:40 +0000 |
| Subject: | Re: ARRAY_UNIQUE_IDENTICAL option | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-118953@lists.php.net to get a copy of this message | ||
> To solve this, I propose adding an ARRAY_UNIQUE_IDENTICAL option that
> can be passed to array_uniques $flags which uses identical operator
> (===) semantics. Internally it uses a new hashmap that allows using
> arbitrary PHP values as keys to efficiently remove duplicates. This is
> slightly over-engineered for this use case. However, this data
> structure will be required for implementing ADTs to deduplicate
> instances with the same values. This hashmap is a heavily minimized
> version of the teds extensions StrictHashMap [1].
As a regular developer, I look from the usage side, not from the
implementation side.
From such perspective, remembering that filtering unique enums
requires this exact flag
is somewhat "dirty": on the surface, nothing tells that enums are
non-comparable.
You will have to memorize yet another PHP quirk, or be able to build a
logical chain:
- enums are non-comparable by default
- enums have no default string value (if not baked by a string)
- array_unique internally sorts an array
- default flag for array_unique compares the string representations of its items
- thus it won't work for enums in a general case
This requires deeper knowledge of PHP, which is for sure a valuable
skill, but not as wide-spread
as we would like to. Most of the devs will first try to use
array_unique() with the default flag, perhaps
not even knowing about flags existence. Each usage of such a flag will
not reveal intent by itself
and at least will require a comment for readable codebases.
Perhaps an alternative idea is to provide a default string value for
enums which are not baked,
Nikolas had already brought up this idea earlier.