Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115610 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 28740 invoked from network); 30 Jul 2021 10:18:07 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Jul 2021 10:18:07 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1E84E1804C9 for ; Fri, 30 Jul 2021 03:45:50 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,BODY_8BITS, NICE_REPLY_A,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS35329 80.237.132.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wp160.webpack.hosteurope.de (wp160.webpack.hosteurope.de [80.237.132.167]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 30 Jul 2021 03:45:48 -0700 (PDT) Received: from [2a02:8109:9d40:1d44:c6bc:6e65:35f4:7a3a]; authenticated by wp160.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) id 1m9Q1S-0002Cw-Qq; Fri, 30 Jul 2021 12:45:46 +0200 To: internals@lists.php.net References: <1627603260.540422871@f450.i.mail.ru> Message-ID: <306bf129-c00f-5f3e-b2de-e80afdf85d72@mabe.berlin> Date: Fri, 30 Jul 2021 12:45:45 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <1627603260.540422871@f450.i.mail.ru> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-bounce-key: webpack.hosteurope.de;marc@mabe.berlin;1627641950;b94e4f3c; X-HE-SMSGID: 1m9Q1S-0002Cw-Qq Subject: Re: Fwd: Re[2]: [PHP-DEV] PHP 8.1 enum const expressions problem From: marc@mabe.berlin (Marc Bennewitz) 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). Marc