Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum returns incomparable instances #341

Closed
petrknap opened this issue Nov 6, 2023 · 1 comment
Closed

Enum returns incomparable instances #341

petrknap opened this issue Nov 6, 2023 · 1 comment

Comments

@petrknap
Copy link

petrknap commented Nov 6, 2023

Hi,
I got to a project that uses these enums and I found out that they cannot be natively compared with each other. To be sure, I compared all combinations of createable instances against each other.

final class Foo extends BenSampo\Enum\Enum
{
    public const BAR = 0;
}

$factories = [
    '__construct' => function () { return new Foo(Foo::BAR); },
    'fromValue' => function () { return Foo::fromValue(0); },
    'fromKey' => function () { return Foo::fromKey('BAR'); },
    '__callStatic' => function () { return Foo::BAR(); },
    'coerce#value' => function () { return Foo::coerce(0); },
    'coerce#key' => function () { return Foo::coerce('BAR'); },
];

foreach ($factories as $ad => $af) {
    $a = $af();
    if ($a instanceof Foo) {
        foreach ($factories as $bd => $bf) {
            $b = $bf();
            if ($b instanceof Foo && $a !== $b) echo "{$ad} is not same as {$bd}\n";
        }
    } else echo "{$ad} is not instance of Foo\n";
}
__construct is not same as __construct
__construct is not same as fromValue
__construct is not same as fromKey
__construct is not same as __callStatic
__construct is not same as coerce#value
__construct is not same as coerce#key
fromValue is not same as __construct
fromValue is not same as fromValue
fromValue is not same as fromKey
fromValue is not same as __callStatic
fromValue is not same as coerce#value
fromValue is not same as coerce#key
fromKey is not same as __construct
fromKey is not same as fromValue
fromKey is not same as fromKey
fromKey is not same as __callStatic
fromKey is not same as coerce#value
fromKey is not same as coerce#key
__callStatic is not same as __construct
__callStatic is not same as fromValue
__callStatic is not same as fromKey
__callStatic is not same as __callStatic
__callStatic is not same as coerce#value
__callStatic is not same as coerce#key
coerce#value is not same as __construct
coerce#value is not same as fromValue
coerce#value is not same as fromKey
coerce#value is not same as __callStatic
coerce#value is not same as coerce#value
coerce#value is not same as coerce#key
coerce#key is not same as __construct
coerce#key is not same as fromValue
coerce#key is not same as fromKey
coerce#key is not same as __callStatic
coerce#key is not same as coerce#value
coerce#key is not same as coerce#key

Even instances created in the same way are not same.

I don't quite understand the point of an enum whose members cannot be compared (matched, switched,...), so please add a big warning about this strange behavior at the beginning of README.md.

spawnia added a commit that referenced this issue Nov 28, 2023
spawnia added a commit that referenced this issue Nov 28, 2023
@spawnia
Copy link
Collaborator

spawnia commented Nov 28, 2023

I documented this behaviour in #344. It probably would have been best to use singletons when instantiating enums, but introducing that now would be a massive breaking change. Given this library is in maintenance mode - see #332 - it will not change this anymore.

@spawnia spawnia closed this as completed Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants