PHP 8.1 enum const expressions problem

From: Date: Thu, 29 Jul 2021 22:16:13 +0000
Subject: PHP 8.1 enum const expressions problem
Groups: php.internals 
Request: Send a blank email to internals+get-115607@lists.php.net to get a copy of this message
Hello internals! I apologize if such a discussion has already taken place, but I didn't find anything like it.   When working with enums, I ran into problems that are currently not resolved. In some cases, enumerations require self references and constant arithmetic expressions:   enum Example: int {     case A = 0b0001;     case B = 0b0010;     case C = 0b0100;       public const EXAMPLE_MASK = self::A | self::B; // << Invalid expression     public const EXAMPLE_MASK2 = self::A->value | self::B->value; // << Same }   Similar examples can be taken in the existing PHP code, for example, for attributes (Attribute::TARGET_ALL) which is a binary mask for existing «targets». Thus, if targets for attributes were implemented through enumerations, and not through constants, then it would be impossible to implement such functionality.   In addition, enumeration values are not part of constant expressions, so implementation through "Example::A->value" is also not available.   Can you please tell me, maybe there have already been some discussions of this problem? And if not, then maybe we should solve this problem before PHP 8.1 release? Since it seems to me that the lack of such functionality is quite critical.   -- Kirill Nesmeyanov  

« previous php.internals (#115607) next »