Re: Fwd: Re[2]: [PHP-DEV] PHP 8.1 enum const expressions problem

From: Date: Fri, 30 Jul 2021 10:45:45 +0000
Subject: Re: Fwd: Re[2]: [PHP-DEV] PHP 8.1 enum const expressions problem
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-115610@lists.php.net to get a copy of this message
On 30.07.21 02:01, Кирилл Несмеянов wrote:
Yes, I saw this short description in your RFC ( https://wiki.php.net/rfc/enumerations ) about «enum sets». However, I do not quite understand why we can not now add a cast to scalars (string and int) and math expressions, which would solve this problem? This behavior has already been implemented for GMP objects. $mask = gmp_init(0b0001) | gmp_init(0b0010); // object(GMP) { num = 3; } echo $mask; // 3 $mask = $mask + 1; // object(GMP) { num = 4; } $mask instanceof \GMP; // true I mean, for such cases, we can create a new "virtual enum case" containing a new value instead special «EnumSetCase». enum Some: int {
     case A = 0b0001;
     case B = 0b0010;
} var_dump(Some::A | Some::B); // enum(Some::@anonymous) { value = 3; } I don’t think that it is necessary to consider the «enum sets» as a separate case, cause addition is also a fairly popular case: case LAST = self::B + 1; Like any other mathematical operations.
I think you missing something Consider this example: enum Some: int {
    case A = 0b0001;
    case B = 0b0010;
    case C = 0b0011;
} var_dump(Some::B | Some::C) This should result in a set of B|C but with your logic it's the same as just C. and it also needs to work with strings: enum Some: string {
    case A = 'a';
    case B = 'b';
    case C = 'c';
} This is where enum sets comes into play. Without having PHP internals C knowledge I think it should be possible to introduce an EnumSet which internally handles a bit array where each bit is pointing to the position (ordinal) of an enum case but I don't think the ordinal position is guarantied to be stable over processes/versions so this would not directly by serializable nor do we have generics to define an enum set of a specific type (ala EnumSet<Some>). Marc

« previous php.internals (#115610) next »