Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112432 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 23774 invoked from network); 5 Dec 2020 19:52:46 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Dec 2020 19:52:46 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B3AE71804C9 for ; Sat, 5 Dec 2020 11:21:16 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 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 ; Sat, 5 Dec 2020 11:21:15 -0800 (PST) Received: from ip5f5bd23e.dynamic.kabel-deutschland.de ([95.91.210.62] helo=[192.168.178.29]); authenticated by wp160.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) id 1kld7K-000511-Ak; Sat, 05 Dec 2020 20:21:14 +0100 User-Agent: Microsoft-MacOutlook/16.43.20110804 Date: Sat, 05 Dec 2020 20:21:13 +0100 To: Rowan Tommins , Message-ID: <9EA6E41D-B41F-4DF7-9A11-225DE74CED14@mabe.berlin> Thread-Topic: [PHP-DEV] [RFC] Enumerations References: In-Reply-To: Mime-version: 1.0 Content-type: text/plain; charset="UTF-8" Content-transfer-encoding: quoted-printable X-bounce-key: webpack.hosteurope.de;marc@mabe.berlin;1607196076;b1591a51; X-HE-SMSGID: 1kld7K-000511-Ak Subject: Re: [PHP-DEV] [RFC] Enumerations From: marc@mabe.berlin (Marc Bennewitz) =EF=BB=BFAm 05.12.20, 16:21 schrieb "Rowan Tommins" : (Apologies if this posts twice, I may have accidentally hit send before= =20 I'd finished writing it) On 05/12/2020 14:21, Marc Bennewitz wrote: > * Do cases have a stable ordinal number / position and is that access= ible? > This would be very interesting on implementing an optimized EnumSe= t using bit-array / bit-set internally If you want to associate a stable value with each case, you can use a=20 "primitive-equivalent enum" and assign each case an integer manually. A= s=20 the RFC says: > There are no auto-generated primitive equivalents (e.g., sequential=20 integers). I think that's a good design decision, because the language itself can'= t=20 actually guarantee a stable ordinal number if the enum's author makes=20 changes; e.g. given: enum Colour { case RED; case GREEN; case BLUE; } somebody might decide the cases should be in alphabetical order: enum Colour { case BLUE; case GREEN; case RED; } It seems unnecessarily confusing to have this be a breaking change=20 because code somewhere relied on the ordering. It's much clearer if the= =20 author has to declare an intentional value, and can then maintain it=20 when making cosmetic changes to their own code: enum Colour { case BLUE=3D3; case GREEN=3D2; case RED=3D1; } Sorry for the confusion - I mean stable within the same process - not over = different processes / systems. > * I often use metadata in enumerations and so I would be very=20 > interested to allow constants. Could you give an example what you mean? Metadata on individual cases i= s=20 supported by methods, which map more cleanly to things like interfaces,= =20 and the notion of each case as a singleton object, not a static class. That said, I have a related question: can enum cases be used as the=20 *value* of constants? e.g.: class OldMaid { public const SUIT =3D Suit::Spades; public const VALUE =3D CardValue::Queen; } If so, this leads to an interesting use case for constants on the enum=20 itself (not the cases) to define aliases: enum Suit { case Hearts; case Diamonds; case Clubs; case Spades; const Tiles =3D self::Diamonds; const Clovers =3D self::Clubs; const Pikes =3D self::Spades; } // The constants and cases can be used interchangeably, since constants= =20 and cases are de-referenced with the same syntax assert(Suit::Spades =3D=3D=3D Suit::Pikes); I mean on mapping something to something else defined as a single assoc arr= ay constant. Something like: enum Role { case User, case Admin, ... } enum Action { case Order_Edit, case Order_Read, =20 private const BY_ROLE =3D [ Role::User =3D> [self::Order_Read], Role::Admin =3D> [self::Order_Read, self::Order_Edit], ]; public function isAllowed(User $user) { return in_array($this, self::BY_ROLE[$user->role]); } } Regards, --=20 Rowan Tommins (n=C3=A9 Collins) [IMSoP] --=20 PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php