Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124975 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 5CB0A1A00B7 for ; Fri, 16 Aug 2024 14:17:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1723817960; bh=LMLwymFDwGsaAQrtDEbiht3ydnGFzNiVXeDyYtu15J4=; h=Date:From:To:In-Reply-To:References:Subject:From; b=jLXUgv13HmqEtLUtE02hNEzQVd65yCQ2pIcw6i3NHosasRrv5HYzeUaj8ebEo+P14 Qj5MJvxlrMRv5ud/5xohQHlJclNpdUcqs3i2kq0rU6hsRU3aX7McP48dENvpqGt2nu BhZfh7rdBQy7B6vEpfV1hdcHlnCqU7CGoTFgQlqhHdlGnD/RNjirBzaD/dJBev1rLD z76QFt6RZcInIi1aIMDcV+ykju/msbu/xSMw298pWpCd1ECLm8sWAzjsbrMCUFFxx/ xjGiMbND5/z0aINpqbn+h+SoMjI/IsGalA3Qd9L/4jVNtDNqi+73G0udmuQjVa/Ae7 dMtpSZvDTi89A== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4282518006F for ; Fri, 16 Aug 2024 14:19:19 +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,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout2-smtp.messagingengine.com (fout2-smtp.messagingengine.com [103.168.172.145]) (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:19:18 +0000 (UTC) Received: from phl-compute-03.internal (phl-compute-03.nyi.internal [10.202.2.43]) by mailfout.nyi.internal (Postfix) with ESMTP id 1396F138FB5F for ; Fri, 16 Aug 2024 10:17:31 -0400 (EDT) Received: from phl-imap-06 ([10.202.2.83]) by phl-compute-03.internal (MEProxy); Fri, 16 Aug 2024 10:17:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding: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=fm3; t=1723817851; x=1723904251; bh=iyxXzgPY9P7WsKItcJIXe kACnSm9ariu9n7geet256w=; b=miRLLqqd1uktTrsXjnPViQOwRcC0avI45MiJ0 vgs1q6P2jf1PTUJaPvG2aqxSrIhVslOL4025jY9tejD+5huzfmtFmfgR960+wNWl sAkzi3WAN7EoTeUZp83S9g4ST2FJUM2d5fSvZ53MJ6GxwQWgFvy/deKwHIaPoBM7 3LrgtPf6Tr63yitSiwuc+aRMc748k9xxb6lWFygMElxf/bBUeR7e1eJp9E7HLZhO 8eOiChAu0zQYOYzBUGUy49Da3jqCYmSENLZY6fkhmMXALsg6CD7cypYH74yk+08N 3CNLS66HCs+pxqFoWEyeB+w/3pOh3rS8dJOC3JkHzqK+Yrzig== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding: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=1723817851; x= 1723904251; bh=iyxXzgPY9P7WsKItcJIXekACnSm9ariu9n7geet256w=; b=i SR9moKBWoz4dwqR7ttEkPODqLTYXohFwGfDf3nBiRW5CjoFBVunwf9D4nRpnyxmK Haiq9323hRrH0IvYGczuwuvVmDouzXyO2fvAvIEwDUchYPk9PVXaYFb2HYXg0Dgw xo3crtPYtsHketVOb91smsIG7K0GkLZqccDcwf0t+sSMAzPSbuafShwXP+9xkkyq LfR/Kg3IpFQq25WvrPf81GJ/5t0pYKQXSSwfHQw/KC2bLIi2QP1bbkyfL/+6Zn8H E6xlMRgnnD+/E4jzKYrXeL6LzGmMEp7ub45TIup0FyT/eYK/lDryW64gI3t5iroH 1DuC6yuQjgZ/LyUg6u9Ww== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddruddtkedgjeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepofggfffhvffkjghfufgtgfesthhqredtredtjeen ucfhrhhomhepfdfnrghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfih gvlhguthgvtghhrdgtohhmqeenucggtffrrghtthgvrhhnpeffieeivdfhvdeguddttdeg teeiueegvefhteehfeeffeetudeitdehtdegjeeuieenucevlhhushhtvghrufhiiigvpe dtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggt hhdrtghomhdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpdhrtghpth htohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id AD055940067; Fri, 16 Aug 2024 10:17:30 -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 09:17:10 -0500 To: "php internals" Message-ID: <79817594-f9a9-4f02-b48a-4bf39e4372e6@app.fastmail.com> In-Reply-To: References: <34548342633f1d87b2fc77c643bd895a18f71b66.camel@ageofdream.com> Subject: Re: [PHP-DEV] [DISCUSSION] Class Constant Enums? Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") 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 of 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, Swift= ,=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 properties=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 want=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 I agree with Alexandru. Since enums are 90% syntactic sugar over classe= s, "inner enums" would be 80% of the way to "inner classes". And I woul= d 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, w= hat is allowed to extend from what, etc. But that would support inner e= nums as well. Based on our sibling languages (Java, Kotlin, C#, etc.), the syntax woul= d likely be something like: class Outer { private string $foo; public function __construct(protected Sort $order) {} enum Sort { case Asc; case Desc; } class Inner { public function __construct(private string $baz) {} } private class HIdden { public function __construct(private string $baz) {} } } Which enables: $case =3D Outer::Sort::Asc; $o =3D new Outer($case); $i =3D new Outer::Inner('beep'); $h =3D new Outer::Hidden('beep'); // Visibility error I would have to research to see if other languages did this, but one opt= ion would be to allow an inner class to extend an outer class even if it= 's final, which would essentially give us sealed classes for free: final class Outer { class InnerA extends Outer {} class InnerB extends Outer {} class InnerC extends Outer {} } // But this is still not OK: class Sibling extends Outer {} Note: I have no idea how difficult/complicated this would be, but I woul= d be in favor of exploring it. --Larry Garfield