Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112436 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 45117 invoked from network); 5 Dec 2020 23:55:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Dec 2020 23:55:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 73F0B1804DB for ; Sat, 5 Dec 2020 15:23:49 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.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 15:23:49 -0800 (PST) Received: by mail-ot1-f42.google.com with SMTP id 11so9016613oty.9 for ; Sat, 05 Dec 2020 15:23:49 -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 :cc; bh=X0Az/jXmXCOm6jOi+d7GNegt+U6k5sQ0z+M1qYA6ARE=; b=lkBsAdZBQnUWrUEwBE0nwsmNKKO2Tm3jiX5CGPsubUyeqBEuZ3UWe0U5vzal9GJfD8 ZY+aZ6gyg9+xnLPkVFYDTGYZUzXU2a1n+lQQ/MyhxGPUEd2dcIavQTI5RN76bN8M2FaJ lUvppabaBXWpOMKpzFt1b/ROjgstz0mI8SYyb9nsdR4PScNncNxBU6ZxfJhoAIAt+xOF FmNbF6IrXMhly30xC2NQijC6z1YUDY6tqcxEy3hjW/qqVfVzVbRofaqeG4BAUun+yYMy WBGyeCvp/I+SbsEfCXz2IoD2AvSbk8NDIg/n/xb5AniNsXIt1aSSpCx7gL7/dqlDyUid nJFA== 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:cc; bh=X0Az/jXmXCOm6jOi+d7GNegt+U6k5sQ0z+M1qYA6ARE=; b=CE/OT8xuoS6Y7Lxp0cqyLQo9CrIb3178RsohtkMHzEP1TUsqeVUcpMmEGqhokxdc3B baW2S52+BioAUNFUMG+LN/2WR6+UcXGTWa/U444MJO9CL5qK9lM6pLspjA26jwbrxVsP ONqR9HJ4Rlj1HJJO/wRSBOGxnrVO8E1q1IGK6nOlDnFsIA/4evjvJDIhbvQsrTevwLLP zmRRdbmZZQE7XGa/4VpziOl2Q9tGtR/511c4paCrfpaPR8QFsDV+w2ygmho/rcwbB+tf 4KV+sbk3Ct9Og0IvPpcWbwr2JZHtcdtRA7UkHv2GyXhVruRg0xoD+LN4jbXcyoEVxfse lCaA== X-Gm-Message-State: AOAM531U57rK+DedJHw8E4JUP93LWCdY4qshQTKSDDRHJegMoUSzCv29 Xq0yH/jVlK9Ode+W3+S/cFNm8T5XbfV11i91RbrW7yr9HTdX+w== X-Google-Smtp-Source: ABdhPJzy8Do98Gjw9bBUsvocnBqSDZU3CF8MR0gG0w6p6AYAfdZde4ILW1Z+RqiVQwEiDCxCjynICji7fHhyW3/nOf4= X-Received: by 2002:a05:6830:22f9:: with SMTP id t25mr8522752otc.14.1607210628476; Sat, 05 Dec 2020 15:23:48 -0800 (PST) MIME-Version: 1.0 References: <79044037-4cd4-4065-a3c1-39a4deea3e09@www.fastmail.com> In-Reply-To: <79044037-4cd4-4065-a3c1-39a4deea3e09@www.fastmail.com> Date: Sun, 6 Dec 2020 00:23:37 +0100 Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="00000000000096f4e305b5bfe2e0" Subject: Re: [PHP-DEV] [RFC] Enumerations From: benjamin.morel@gmail.com (Benjamin Morel) --00000000000096f4e305b5bfe2e0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thanks a lot for this RFC, Larry and Iliya! I can't imagine the amount of thought and work put into this. Enums are definitely a most-wanted PHP feature. I played a bit with the early implementation, and love it so far. Here are my thoughts on the RFC and the current implementation: *Serialization* I guess what it comes down to is whether / how easily a class can return > an existing instance when asked to unserialize, rather than setting > properties on an existing instance. That is, given the string > "C:4:Suit:6:{Spades}" can the class definition return the appropriate > singleton for Suits::Spades rather than a newly constructed object? > > If this proves tricky to implement, it would probably be better to > forbid serialization than using the default object format and breaking > the singleton-ness of the case objects. +1, totally agree with this statement. That's the first thing I tried when playing with the implementation, and noticed that serialization is not supported: echo unserialize(serialize(Status::Active)); Notice: unserialize(): Error at offset 0 of 42 bytes I would definitely expect strict equality to be maintained on enum cases even after unserialization! *var_export()* Currently, var_export() returns a __set_state() syntax: var_export(Status::Active); Foo\Bar\Status::Active::__set_state(array( )) I guess this should just return Foo\Bar\Status::Active. *Scalar Enums, ::cases()* The implementation does not support these yet, so I haven't had a chance to play with them. I share Pierre R.'s concerns, though: Does this mean that an enum can't have two cases with the same primitive > value ? I would very much being able to do so, for example, when you > change a name and want to keep the legacy for backward compatibility. But I'd understand if you just disallow duplicate scalar values altogether, this is probably the most sensible solution here. Another idea that comes to mind is that cases() could return an iterator instead of an array, having the cases as keys and the scalars as values, but this would probably come as a surprise and be bad for DX. *Enum & UnitEnum interfaces* The implementation does not seem to support these yet. Taking the examples from the RFC: Suit::Hearts instanceof Enum; // true =3D> Parse error: syntax error, unexpected token "enum" Suit::Hearts instanceof UnitEnum; // true =3D> FALSE Best of luck with the RFC! =E2=80=94 Benjamin --00000000000096f4e305b5bfe2e0--