Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112505 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60223 invoked from network); 14 Dec 2020 23:13:26 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Dec 2020 23:13:26 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BEC751804D4 for ; Mon, 14 Dec 2020 14:44:13 -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.6 required=5.0 tests=BAYES_50,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-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) (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 ; Mon, 14 Dec 2020 14:44:13 -0800 (PST) Received: by mail-oi1-f173.google.com with SMTP id s2so21122896oij.2 for ; Mon, 14 Dec 2020 14:44:13 -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:content-transfer-encoding; bh=xJE9kTLkOhcc4RWBK9afx6URGJcNb8+d1GQ/Vsw0u9c=; b=Z0gsZHVDD4eoqPLJ/95+F3c1Uoa2Ju7ADMkDOPSHPFzVvtnxgPPD9oeixjK8K7gwXh pwI1kZrYBMxNdw35dP6N4A8ULiBTCa+yaJm9l+9Z1DT4IyKPp2SQvufedBQMMLUIcaro fvHJKts7HJcmgwRjHrdJ0nI+vP4fRUiVdKZieSixjFSEARSzYuUb2pwK90/Gjny3UwTc Vp0lg081Akqgrmlirxv8weka01Avy+z3dhd5OadCk1h/MqAqJchnONHSyZCT7DLp2BOj gA2z4uRL1wbnJUtpEAO3v3bVNIK//yUQ/uHyaQl0TcbP6xkW0hH0clUpTXsrfLknRdg9 nytA== 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:content-transfer-encoding; bh=xJE9kTLkOhcc4RWBK9afx6URGJcNb8+d1GQ/Vsw0u9c=; b=Gb3DVxGXoOb2Q8Je99FvYwy258jBY8gPLQeR8u+RKHUh/DqS1F3YCkw7JL+srDqcAt OpmpwOscvAUTu9WjuLbME2FU8+wDzGKoL/YPr6LRevLVizeuQcVDrBLk352ozgQhjdNY mteY3kaxn2wlNgWT7vj0LPtQUdg8sSYjzwopUgOadKTz4gx7jvPJdTDdmT5UwQ3BsISL 24EFshxxEhDiTeVmGND3OvzG6BbGmnTdkQzDQPaS1bQ92IC4ZW6D81V/blLvzR0rw4UF RzC07NVHjvME9mJ37gyFB7PX+k+fma/9cGVSKdJptcA5dtis1HxBF1tDsLQTQ4o24VoX gW/w== X-Gm-Message-State: AOAM530anTsvemHYSJKKsJxL/rSn/kQGbQz+Q83QY+KtM6WDldbvAYzZ 3E37x4RJraZ+RX12PJEYYv9+K8FFE+2foYJ/nxOfycq63LhO X-Google-Smtp-Source: ABdhPJxzpE3Bo6yz0sUvIod2Syh5cl8XCkHaO86RX6j9H2EFGAIhSx+q/UCc/1H6HygbhjlRAdNWkTqrxvq6D+l9stQ= X-Received: by 2002:aca:afd0:: with SMTP id y199mr19756749oie.7.1607985851447; Mon, 14 Dec 2020 14:44:11 -0800 (PST) MIME-Version: 1.0 References: <32277b43-079d-4cd7-a159-8ad555096742@garfieldtech.com> <31c9d66b-771a-35ed-1b11-c681bbcc02c3@gmail.com> <5e92a320-49fc-41b0-8969-338d62c98419@www.fastmail.com> In-Reply-To: <5e92a320-49fc-41b0-8969-338d62c98419@www.fastmail.com> Date: Mon, 14 Dec 2020 14:43:56 -0800 Message-ID: To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] Enumerations From: paul.crovella@gmail.com (Paul Crovella) On Tue, Dec 8, 2020 at 10:14 AM Larry Garfield wro= te: > > > Hi Paul. Although we're on hold for a bit while Ilija makes some changes= in direction (see previous email) I'm looking forward to seeing the results. > > Enumerations, as a general concept, are stateless. Or rather, the idea o= f state doesn't even apply to them. They're (enumerated) lists of things. You need a way to make a list, identify an item as being on the list, and differentiate it from other items on the list. > The integer 5 doesn't have any state of its own. All instances of the in= teger 5 are logically identical to all other instances of the integer 5. For simple types like integers, sure. Objects however can carry additional data around without interfering with identifying or differentiating them. It doesn't matter if object 5 has my address book, is wearing a funny hat, or just cached the results from a bunch of expensive method executions - you can still identify it and distinguish it just fine. > That the PHP RFC for enums uses objects internally is incidental. This is a complete reversal from what you've otherwise written in and about the RFC. > So if you're defining, say, an enum for cardinal directions (North, South= , East, West), There are literally only 4 possible values for that type, an= d those values must always be equal to themselves. Attaching "5 miles" to = "South" in one script and "2 km" to "South" in another script is nonsensica= l, just like attaching "feet" to the number 5 and later changing it to "fin= gers" just doesn't make any sense at all. Whether or not an object holding other data makes sense is irrelevant to enums. Take it up in code review. > It is true that, if you're attaching operations to enum cases that have n= o state, a method, a static method, and a constant are often interchangeabl= e. A constant is strictly less capable since it cannot have logic, but a m= ethod and static method are functionally equivalent. The reason for forbid= ding static methods and constants is largely simplicity. You've significantly added to the WTF factor and edge cases when working with them. For example enums can implement interfaces. Except interfaces with constants. Or maybe you can implement interfaces with constants, you just don't inherit the constants. Wait no that'd break type declarations. Perhaps then you can inherit constants from interfaces but you still can't declare them yourself. So the way to use constants in enums is to declare them on an interface and implement that to inherit them. Maybe. This isn't simplicity. None of these options makes working with enums bette= r. Whether you prefer constants, static methods, or instance methods isn't even relevant to enums. Again take it up in code review. > For example, the change I just discussed in my previous email about switc= hing to a single class with instances as constants means that static method= s can only be on the enum (Suit), because there is no per-case-class to put= them on (Diamonds). The same is true of per-instance constants. If we'd = promised that those things worked, we wouldn't be able to make this change. The thing never shipped, you can make any change you want. If you're pretending it did ship then you couldn't make that change anyway without breaking instance methods so your point is moot. > If when the dust settles there's a good reason to expand the functionalit= y further in ways that still don't violate "South =3D=3D=3D South," (such a= s adding constants to the enum type itself, perhaps) those can be considere= d at that time. As you've said elsewhere: > Because enums are based on classes, there's not really any added complexi= ty. They inherit the ability to have methods from being classes. It would b= e more work to separate it. The same holds for constants, static methods, properties, (traits? do these even work? some of them?) This isn't about adding functionality, it's about not arbitrarily removing it. > As both Rowan and I mentioned, the long-term plan includes future RFCs fo= r other functionality such as tagged unions. If you want state, I want a coherent enum RFC that isn't complicated by unrelated personal preferences being shoehorned into it. If you want to make an RFC to deprecate class constants in favor of methods - fine, but at least make the RFC about that. Trying to wedge it in here half-assed only creates problems. > then what you want is not an enum but a tagged union. You're saying this as if they're necessarily different things. > For whatever reason, most languages with tagged unions seem to build them= into and onto enums. Languages that implemented enums such that they function as tagged unions saved themselves the effort of making the same thing twice. They also saved their users from dealing with whatever subtle differences would've been included to distinguish them. > I'm not entirely sure why, and I'm honestly not 100% convinced that's the= right approach in PHP, but that's a debate for a future RFC. Any debate about enums is appropriate for this RFC. > Within the scope of this RFC, enums must always obey "South =3D=3D=3D Sou= th", and thus must be stateless. South =3D=3D=3D South can hold with or without state and isn't even require= d for enums. All you need is to know it's on the list and for it to be distinguishable from others on the list. > > Longer term plans are irrelevant except to avoid inadvertently > > shutting the door on something. This RFC is up for discussion, and > > will be up for voting, in isolation. It has to be able to stand on its > > own. > > This is true up to a point. The point is nothing in the RFC is contingent on the others. If this RFC passes and none of the others do, or none of the other work gets done, that has to be okay. This means treating this RFC on its own. As you said at the beginning: > The overarching plan (for context, NOT the thing to comment on right now) Yet you keep bringing it up... > Explicitly setting up a series of RFCs like we're doing is, as far as I a= m aware, novel for PHP. It's the correct way to handle a larger task like = this, however, especially when there clear division points that "chunk" the= work in natural ways. Even if enums in this RFC are the only thing that p= asses, that's still a major win for PHP. It's not as big a win as tagged u= nions with robust pattern matching and generics on the side would be, but i= t's still a big step forward. By breaking it into chunks, we make it more = likely that as much as can be done will get done, and approved, and into th= e language, even if the end result is the same feature set when 8.1.0 gets = tagged. > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >