Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112727 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 72512 invoked from network); 3 Jan 2021 12:18:39 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jan 2021 12:18:39 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 53C101804D3 for ; Sun, 3 Jan 2021 03:54:21 -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_20,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-yb1-f172.google.com (mail-yb1-f172.google.com [209.85.219.172]) (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 03:54:20 -0800 (PST) Received: by mail-yb1-f172.google.com with SMTP id x2so23364492ybt.11 for ; Sun, 03 Jan 2021 03:54:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=ZQ9b9j4g+Z/K12sXwkfNpndPwLgszGRZJXqeXjpM4uU=; b=HuJkxpytE/lJ7lLdGLurvpYtJwfUjdfyo6m0eM3Bd1dbY895DTOecMUNGxFbEGLG0S ADE7eMQnQD188bc8kmOUMRFnOj0Bw2JdEciH5a+ONnjRLIWJEkiE9osQGXIaDzcnPU/c PQt2oNoszSNzhaSMFKc+bBeGrjOEOjruBn3T5vZhV1pEshkxxWYkwWgnaAzryMP296fN UzcNWnrg0b+ecnEGP/J5okP7ffTwX0Jqz5FNGZms46Ep3rPRg3jNWVfZlZhJk2OXZAqf VEwUdGCDcHgZoGRIhLmOgn6tgEZ5m/GkeXRGKPdettldLuzxqXf6hssa2NMmC1p9Rgc1 EYUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=ZQ9b9j4g+Z/K12sXwkfNpndPwLgszGRZJXqeXjpM4uU=; b=lEuIfSexI3vmsnyTQ7g49A1EUTVO3zX7fk0AtDQH4ouUYUc20Kos4Kqiuqc00ko4Q4 v3vjLd7bY7fMWPhuv7SLt6r9QdiTlMULGQLbZUIk3JakHPK5+hZlgOcpXH8VNB4FACMP 7rgYRslBZ8xXbi6Dfbc5eXizQvADWBJ7yao5POKXO4BEE3lA5cFWs9yuw4Z15nXso0dj 23gcZ3JebH2duGjC/KyNlDcvmqBlfjwkXUlQMzhGhaw8LCKydLFA8Pxvi2Iqq+DlEBJ0 0D70+5P8Ivu+zyPqsApDQdunciguJV7l0SEfwF7AyLj8M7414tkvOGzse+mpUm62w7rN 8gRg== X-Gm-Message-State: AOAM531hbS0fJU94X4wmcd45dup3SonSZ7snsVB2fWkRPLUVuErLNQ0M l2ajKA2RHuWwLwz9lcRduutdGdHgfQxIFpToOlYDLWRz2fNPdg== X-Google-Smtp-Source: ABdhPJzmTRSxnoHMKxaOX/YWMPwH6fWHXtu+JufweBzy/4Vlpb94kOIVhDe6W2DCaoVvH8c5SAtOuMCdcbALuCOQ8d8= X-Received: by 2002:a25:aea8:: with SMTP id b40mr102510238ybj.347.1609674854765; Sun, 03 Jan 2021 03:54:14 -0800 (PST) MIME-Version: 1.0 References: <67c9d05a-f0a7-fcea-32ea-7a4e409308a1@mabe.berlin> <5120eee2-56c4-4608-9e41-4dd05563fef7@www.fastmail.com> In-Reply-To: Date: Sun, 3 Jan 2021 12:54:03 +0100 Message-ID: To: php internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] Enumerations, Round 2 From: tovilo.ilija@gmail.com (Ilija Tovilo) 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. 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. > 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. Ilija