Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112428 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99372 invoked from network); 5 Dec 2020 15:53:06 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Dec 2020 15:53:06 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CC89C1804C9 for ; Sat, 5 Dec 2020 07:21:33 -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=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (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 07:21:33 -0800 (PST) Received: by mail-wr1-f42.google.com with SMTP id g14so8193962wrm.13 for ; Sat, 05 Dec 2020 07:21:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=BHO2R0eG1Q5mvN1w6q8Y8F4xV/A0IyHc63lexbcxoL0=; b=U/vEeSHS5eVLiPO4Eux1ANr4XvRUS07vDTQXS7EPFmkLYnFqpu3AM3h6pFTZ004WZx wlAn2R0yTDAHvXqnrcAjWZL2FHYCP53S2dL2N69nQh5KlAOG6JKNK2RegEJdSEGx0Lkr pCFfsqrICGMgRMLcHoWzNwyVkhjpmqBbahavsxP5bs1fDr18DaVWIRJ7/qjlBJombTwu SyVgD7K0JbBpxplxLa3V2QACRnsF0gw8TbW8MvnwSHylF+ybPSvns5H34ync7LDG1HE4 Sx3FH83J1knxmj+ml2ISedrlLzvnnk0QNoHwgTjXeJbBI5Fz/sIwMQNuAaFfuXQsFzqn DyPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=BHO2R0eG1Q5mvN1w6q8Y8F4xV/A0IyHc63lexbcxoL0=; b=e4vWiSJf04gM1rlRCCwGbjrTDznPrleziW+WMTLN788sahMw1DK2j56aLqYwMl4pK2 L+pMHlAqiFSK1vPhS6GA6qCT39hTHeKZInVd1p+36DX079SrGtltfF+Cz/Xrnqsb4J/t 1tXF0yNOHiqUBBBZR1q1wCHCHJcLntOXwKazRm287tcUD0WJ/f1gqdN6uyljHxIWOged u+aTKQH87i1hS+GIVunPYgQPs2xXL/l8EprWYsfOGwz3sBGzk8pkzA38AbBISi3Ux/D9 KxBPhTDHCYs/9uQGM2vVHclMg2gtg81asLc6qjn7bjLhzy9t+ERB3Im2lKVEpY3hOjzQ 9sQA== X-Gm-Message-State: AOAM531nWSoNa/MyFNH8UZXhnuxES1qThyG34/nYiyhofbrkHWmmYQOj MOZVESS9uRKrxMvmwiZH31Y+IQ3/HF8= X-Google-Smtp-Source: ABdhPJyX5z5QsGHGRePonYYmSpkV367PV0bTLEh2ubjW1ufHStm7wFnM7yZCyI7dhckNOv0bTphong== X-Received: by 2002:a5d:560e:: with SMTP id l14mr10736287wrv.191.1607181691738; Sat, 05 Dec 2020 07:21:31 -0800 (PST) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id c9sm2368505wrn.65.2020.12.05.07.21.30 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 05 Dec 2020 07:21:30 -0800 (PST) To: internals@lists.php.net References: Message-ID: Date: Sat, 5 Dec 2020 15:21:28 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] [RFC] Enumerations From: rowan.collins@gmail.com (Rowan Tommins) (Apologies if this posts twice, I may have accidentally hit send before 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 accessible? > This would be very interesting on implementing an optimized EnumSet using bit-array / bit-set internally If you want to associate a stable value with each case, you can use a "primitive-equivalent enum" and assign each case an integer manually. As the RFC says: > There are no auto-generated primitive equivalents (e.g., sequential integers). I think that's a good design decision, because the language itself can't actually guarantee a stable ordinal number if the enum's author makes 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 because code somewhere relied on the ordering. It's much clearer if the author has to declare an intentional value, and can then maintain it when making cosmetic changes to their own code: enum Colour { case BLUE=3; case GREEN=2; case RED=1; } > * I often use metadata in enumerations and so I would be very > interested to allow constants. Could you give an example what you mean? Metadata on individual cases is supported by methods, which map more cleanly to things like interfaces, 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 *value* of constants? e.g.: class OldMaid {     public const SUIT = Suit::Spades;     public const VALUE = CardValue::Queen; } If so, this leads to an interesting use case for constants on the enum itself (not the cases) to define aliases: enum Suit {   case Hearts;   case Diamonds;   case Clubs;   case Spades;   const Tiles = self::Diamonds;   const Clovers = self::Clubs;   const Pikes = self::Spades; } // The constants and cases can be used interchangeably, since constants and cases are de-referenced with the same syntax assert(Suit::Spades === Suit::Pikes); Regards, -- Rowan Tommins (né Collins) [IMSoP]