Bug #81293 [Opn->Fbk]: Abstract method cases() in trait imoses contract upon the exhibiting enum

From: Date: Sun, 25 Jul 2021 20:24:43 +0000
Subject: Bug #81293 [Opn->Fbk]: Abstract method cases() in trait imoses contract upon the exhibiting enum
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235339@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81293&edit=1

 ID:                 81293
 Updated by:         requinix@php.net
 Reported by:        raincomplain at outlook dot com
 Summary:            Abstract method cases() in trait imoses contract
                     upon the exhibiting enum
-Status:             Open
+Status:             Feedback
 Type:               Bug
 Package:            Class/Object related
 PHP Version:        8.1.0beta1
 Block user comment: N
 Private report:     N

 New Comment:

> I think we should clarify with a proper error message that abstract cases() is
> not allowed too.

But it is allowed.
Test wants cases() to return an int, Size's cases() returns an array. They're
incompatible. Change the trait's method to return array and it works.


Previous Comments:
------------------------------------------------------------------------
[2021-07-25 15:43:03] raincomplain at outlook dot com

Description:
------------
Both Pure Enums and Backed Enums implement an internal interface named UnitEnum. UnitEnum includes a
static method cases(). So manually defining cases() method on an Enum results in a fatal error as
expected(as clarified in enum RFC). However, it seems it's allowed to define cases() in a
trait, resulting in cases() being a requirment in the exhibiting enum with somewhat a confused error
which makes no sense!. I think we should clarify with a proper error message that abstract cases()
is not allowed too. Non-abstract cases() is totally fine since the enum implementation will take
precedance over trait so no error message is needed here.

Test script:
---------------
enum Size: int 
{
    use Test;
}

trait Test 
{
    abstract static private function cases(): int;
}

Expected result:
----------------
PHP Fatal error:  Abstract Size::cases() is already implemented

Actual result:
--------------
PHP Fatal error:  Declaration of Size::cases(): array must be compatible with Test::cases(): int in
/var/www/html/index.php on line 0


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=81293&edit=1


Thread (6 messages)

« previous php.bugs (#235339) next »