Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112738 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16740 invoked from network); 3 Jan 2021 20:50:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jan 2021 20:50:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 01F1B1804D4 for ; Sun, 3 Jan 2021 12:25:47 -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 ; Sun, 3 Jan 2021 12:25:45 -0800 (PST) Received: from [2a02:8109:9d40:1d44:40b6:f5d3:c567:d46c] (helo=nas.fritz.box); authenticated by wp160.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) id 1kw9wd-0000EP-DT; Sun, 03 Jan 2021 21:25:43 +0100 To: Ilija Tovilo , php internals References: <67c9d05a-f0a7-fcea-32ea-7a4e409308a1@mabe.berlin> <5120eee2-56c4-4608-9e41-4dd05563fef7@www.fastmail.com> Message-ID: <964326f1-a3ba-e343-86a6-ea88c33d45c8@mabe.berlin> Date: Sun, 3 Jan 2021 21:25:43 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-bounce-key: webpack.hosteurope.de;marc@mabe.berlin;1609705546;dcbc5a31; X-HE-SMSGID: 1kw9wd-0000EP-DT Subject: Re: [PHP-DEV] [RFC] Enumerations, Round 2 From: marc@mabe.berlin (Marc) Hi Ilija, On 03.01.21 12:54, Ilija Tovilo wrote: > Hi Marc > >> I don't have a really good use-case for float values. It just seems >> weird to me that a ScalarEnum doesn't support all scalars. >> >> Using the enum value as array key for `cases()` works with your current >> proposal but if we later want to allow floats, bool whatever then we got >> a food gun. > The main reason is that we're using a hashmap internally in from() to > find the given case you're looking for. This is the same hashmap PHP > arrays are based on which only supports ints/strings as keys. If we > were to allow any scalar as a value, looking up a case by value would > become a O(n) operation.w > > We could do something terrible like serialize the key before storing > it in the hashmap to allow arbitrary key types. But that will require > serializing the value on each invocation of from() which will > unnecessarily slow down the 95% most common use cases (int/string) to > support the exception. Note though that it's always easier to extend > than to remove. By not offering this feature we're erring on the side > of caution. > > That being said, I can see how ScalarEnum is a misleading name. We've > been thinking about a better name and only had some ideas we weren't > fully satisfied with. RawEnum, ValueEnum and ConvertibleEnum were some > of these ideas. Let us know if you have a better suggestion. That's reasonable and makes sense at this point. Only supporting string and int is also fine (I don't have a personal use case for other types). So using the same HT implementation as arrays internally totally makes sense but this is an implementation detail not visible for the outside and we shouldn't block outself for the future now as nobody knows of unknown possible use cases. At least if we can avoid it. >> You already provide a lookup mechanism with `MyEnum::from()` - I don't >> see a real use-case for proving a pre build map. The main use case I see >> is to list all possible enum values but this doesn't require a map and a >> zero-indexed-array would also be more performant with packed arrays >> (correct me if I'm wrong). > I do somewhat agree with you there. We're essentially returning > `Array|Map` which feels > inconsistent. When you're calling cases() you're most likely going to > loop over it at which point $case->value is available at your > disposal. Would you consider making `cases()` returning a simple list in all cases instead of differentiate between UnitEnum and ScalarEnum given the fact that mostly people just want to loop over cases and a lookup is already available with ScalarEnum::from() to provide a cleaner interface? Marc > Ilija