>Пятница, 30 июля 2021, 13:46 +03:00 от Marc Bennewitz <marc@mabe.berlin>: > > >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 > >-- >PHP Internals - PHP Runtime Development Mailing List >To unsubscribe, visit: https://www.php.net/unsub.php Yes, that's a good example. I agree.
But in addition to bit-masks, there are also at least cases of addition and subtraction (the most popular). Can't we adapt all math expressions existing in nature to specific cases, thereby building our own pseudo-AST? (Some::A + Some::B) * Some::C | Some::D; EnumSet of - a: Some::D - b: EnumMul of - a: Some::C - b: EnumSum of - a: Some::A - b: Some::B Imho, this is a hellish overcomplication. Isnt it? -- Kirill Nesmeyanov