Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124976 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 8D8BE1A00B7 for ; Fri, 16 Aug 2024 14:28:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1723818596; bh=Icipi+aco6ga4xTVQtfIc/eQKFlB7v82j4mmXV+d18k=; h=Date:From:To:In-Reply-To:References:Subject:From; b=cwqWbWHLEfsXE9uL08hCZmicc5VWCqPSf++7KI6Gn0gqoxJ7aIos0a2IvM7Z1EeNT QBWob3WUTyNuoruFU8FLP5zDaGUnekDxBDSzewV6Jy7qODFC0T5byOj9TXTQh6v43V YLP7a21eRjPk17kEDE9gYHQ7o9ZYx0C2+16e9eGiehsvLu+N7qAPS55/ys/x6ObR2t 2ukIWTA1EztHpI2CVIt6qNFXPBNOwlS3xWvFg12mWruIMYmIZKmB3JFBrYP+yhUJiv SPtsUDw0tFF/ArEIlV7qnt7ZkTHL07Fd/VhIL36WhjoMIVPTqQ+5TJD5PEE9MVC3hm 5LlA2IMmhDenQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 71654180079 for ; Fri, 16 Aug 2024 14:29:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh5-smtp.messagingengine.com (fhigh5-smtp.messagingengine.com [103.168.172.156]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 16 Aug 2024 14:29:54 +0000 (UTC) Received: from phl-compute-03.internal (phl-compute-03.nyi.internal [10.202.2.43]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 5668E114EA02 for ; Fri, 16 Aug 2024 10:28:07 -0400 (EDT) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-03.internal (MEProxy); Fri, 16 Aug 2024 10:28:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1723818487; x=1723904887; bh=Tsjid2wDyb Ba0MTZ8pspx2MA8V1/XAtiaLXXUv3i2/A=; b=KZs2RRIAuL/ZVbtwMDo9actBdO lJ9+7pz5S5jaeDlPi3yjhG6Doyu7tHz/XjsWIVFVsYOBRrjgdKP1Ey6wpJkLWl5m C84Wr1VYXJGK4dsLIoAb4KuJ/SrDO4ZjT1LiB3+fkucB/JwNztazznhSh8UefLrE QwM/PHMMkEvlq1SwTIVbghdcKVj4c7euqQEFN8RhrOrfBvOJRyuh+aKjWYNv4AJi Zpve8EEqiEWtUjcpvTegT4q/UjFFTf4e0wWy9Yx3O9akVuVKjAd2h4xKP9tp//hk NK6tcc5VthWfxRzJ3xuX257owcF74djW6YiJGprNLM+RGDuMJR+K1uMN+ZiQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1723818487; x=1723904887; bh=Tsjid2wDybBa0MTZ8pspx2MA8V1/ XAtiaLXXUv3i2/A=; b=AQOJTj3o8Gro868/0oZJ+SeF8ckrOd/zC3I1aoNpFVQP 99hUO6LgjN4IJIfnEXCpKWFo4bmWI4sxWipY3qQUynetljTmDATLZLG8kzVusA+9 op7zjapPlnig2OrRBYh4cqXlXbcLRIaNCAFF9Q/fAT5hpOYy02eSbc6he66pqn7U nCUGeIsvShguCuvNTv1NN9jLYpqQrcHPbb6VXlqgpcPy73cqsWK9tLpm7S1rSV7G Ms/z0YqobmaWoDIFTVES+lzvE7gmCDAs8aHFeDR4+OVNOSLjGmFYIihMdpeIkV1s PYbDkupP1tn1hs+FdwF3RRZ9j76/nJ1a8DHjqax1bQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddruddtkedgjeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefoggffhf fvkfgjfhfutgesrgdtreerredtjeenucfhrhhomhepfdftohgsucfnrghnuggvrhhsfdcu oehrohgssegsohhtthhlvggurdgtohguvghsqeenucggtffrrghtthgvrhhnpedtueejtd ethfeulefhtdelieduteelffdtudelheffgedtieehhfelieejgfevgeenucevlhhushht vghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehrohgssegsohhtthhlvg gurdgtohguvghspdhnsggprhgtphhtthhopedupdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id D44EF15A005E; Fri, 16 Aug 2024 10:28:06 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Fri, 16 Aug 2024 16:27:46 +0200 To: internals@lists.php.net Message-ID: <023ae126-af2e-4bc4-966d-636040d75983@app.fastmail.com> In-Reply-To: <79817594-f9a9-4f02-b48a-4bf39e4372e6@app.fastmail.com> References: <34548342633f1d87b2fc77c643bd895a18f71b66.camel@ageofdream.com> <79817594-f9a9-4f02-b48a-4bf39e4372e6@app.fastmail.com> Subject: Re: [PHP-DEV] [DISCUSSION] Class Constant Enums? Content-Type: multipart/alternative; boundary=ecc9128cad224c1a900a61865f3cdd69 From: rob@bottled.codes ("Rob Landers") --ecc9128cad224c1a900a61865f3cdd69 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Fri, Aug 16, 2024, at 16:17, Larry Garfield wrote: > On Fri, Aug 16, 2024, at 6:35 AM, Alexandru P=C4=83tr=C4=83nescu wrote: > > Hi Nick, > >>=20 > >> Is there any interest in having enums as class constants? > >>=20 > >> I'm often finding cases where I would like to have an enum inside o= f a > >> class, but don't want a free-floating enum that's basically like > >> another class. > >>=20 > >=20 > > ......=20 > >=20 > >>=20 > >> class SSHClient { > >>=20 > >> public const enum CommandResult > >> { > >> case Success; > >> case Failure; > >> case Unknown; > >> case Timeout; > >> } > >>=20 > >> // ... > >> } > >>=20 > >>=20 > >> // Usage: > >>=20 > >> SSHClient::CommandResult::Success > > > > > > I feel this topic could be maybe more broad and be called "nested=20 > > classes" that are already supported in multiple languages: Java, Swi= ft,=20 > > Python, C#, C++, JavaScript, etc. > > > > The syntax you showed is usually identical with what other languages=20 > > use, except that probably the const is unnecessary. > > The nested class can have visibility as sometimes having it private=20 > > makes sense. > > Accessing it through `::` is probably fine, but a deeper look at the=20 > > grammar might be necessary. > > The nested class would have access to parent class private propertie= s=20 > > and methods. > > > > I also mentioned this topic on the subject of defining a type in an=20 > > autoloader compatible way. > > And indeed, a type could also be defined nested in a class if we wan= t=20 > > to support that as well. > > > > Now, this feature is not simple, and I think it needs proper=20 > > sponsorship from someone experienced with internals. > > > > Regards, > > Alex >=20 > I agree with Alexandru. Since enums are 90% syntactic sugar over clas= ses, "inner enums" would be 80% of the way to "inner classes". And I wo= uld be in favor of inner classes. :-) There's a lot of potential benefi= ts there, but also a lot of edge cases to sort out regarding visibility,= what is allowed to extend from what, etc. But that would support inner= enums as well. From recently looking into this for totally unrelated reasons, nested en= ums would be far easier to implement on a grammar level. Enums also have= some constraints that make it simpler than the general =E2=80=9Cnested = classes,=E2=80=9D such as rules regarding inheritance. As for the actual implementation, it=E2=80=99ll be the edges that kill y= ou. I would recommend just doing enums, and keep the scope smaller.=20 --ecc9128cad224c1a900a61865f3cdd69 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Fri, Aug 16,= 2024, at 16:17, Larry Garfield wrote:
On Fri, Aug 16, 2024, at 6:35 AM, Alexandru = P=C4=83tr=C4=83nescu wrote:
> Hi Nick,
&g= t;> 
>> Is there any interest in having enum= s as class constants?
>> 
>>= ; I'm often finding cases where I would like to have an enum inside of a=
>> class, but don't want a free-floating enum that'= s basically like
>> another class.
>= ;> 

> ...<snip>.= .. 

>> 
=
>> class SSHClient {
>> 
>>    public const enum CommandResult
<= div>>>    {
>>   = ;     case Success;
>> &nbs= p;      case Failure;
>>&nb= sp;       case Unknown;
>= >        case Timeout;
>>    }
>> 
>>    // ...
>> }
<= div>>> 
>> 
>> /= / Usage:
>> 
>> SSHClient::= CommandResult::Success
>
>
> I feel this topic could be maybe more broad and be called "neste= d 
> classes" that are already supported in multip= le languages: Java, Swift, 
> Python, C#, C++, Jav= aScript, etc.
>
> The syntax you showe= d is usually identical with what other languages 
>= ; use, except that probably the const is unnecessary.
>= The nested class can have visibility as sometimes having it private&nbs= p;
> makes sense.
> Accessing it throu= gh `::` is probably fine, but a deeper look at the 
&= gt; grammar might be necessary.
> The nested class woul= d have access to parent class private properties 
>= ; and methods.
>
> I also mentioned th= is topic on the subject of defining a type in an 
>= ; autoloader compatible way.
> And indeed, a type could= also be defined nested in a class if we want 
> t= o support that as well.
>
> Now, this = feature is not simple, and I think it needs proper 
&= gt; sponsorship from someone experienced with internals.
&= gt;
> Regards,
> Alex
I agree with Alexandru.  Since enums are 90% syntactic= sugar over classes, "inner enums" would be 80% of the way to "inner cla= sses".  And I would be in favor of inner classes. :-)  There's= a lot of potential benefits there, but also a lot of edge cases to sort= out regarding visibility, what is allowed to extend from what, etc.&nbs= p; But that would support inner enums as well.

From recently looking into this for totally unrelated r= easons, nested enums would be far easier to implement on a grammar level= . Enums also have some constraints that make it simpler than the general= =E2=80=9Cnested classes,=E2=80=9D such as rules regarding inheritance.<= br>

As for the actual implementation, it=E2=80=99= ll be the edges that kill you.

I would reco= mmend just doing enums, and keep the scope smaller. 

=
--ecc9128cad224c1a900a61865f3cdd69--