Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123392 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 9B96E1A009C for ; Tue, 21 May 2024 23:31:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1716334352; bh=tQaFHCdvtM0cIjPGqfVE8iT1HZgzlS3W3CXUHsE2TjA=; h=In-Reply-To:References:Date:From:To:Subject:From; b=chcnPsqWWMdSc0t8YDZoBd2TGAEtRmCu7u7bmRTr+1YaCsZwVciMiecE6IpgwOa4c cG8BoajoZSIV8i+8/8NJF8ni3ArlbGx1VYplUBNaiqP3lFNVY+WqGLTh3QvT8eU2So kOEmohBpvrOCCeqAEEPV2aCNYyXzALLiPb0ilzm0XyJ2BRMyJ3FPp66eDheH/RwGh0 esLgQ28b9eos6LW9viSpnYvsAEIN2Q3mV8+lKXuiUSZQEusPe8/FxxACpWRSBuqyUl dSwBWoukN9QFqTirBfraN7QxHdmAKrBggFdTJlb9cjgN+WViColcjbEBPTbfvro9pt N0VSVgyjocolQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A19AE180546 for ; Tue, 21 May 2024 23:32:31 +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,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fout8-smtp.messagingengine.com (fout8-smtp.messagingengine.com [103.168.172.151]) (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 ; Tue, 21 May 2024 23:32:31 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.nyi.internal (Postfix) with ESMTP id C42D7138027E for ; Tue, 21 May 2024 19:31:34 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Tue, 21 May 2024 19:31:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; 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=1716334294; x= 1716420694; bh=fCKUVZvcRp3AgK4oNHuSew3UHpM3jQ6yGJ19octUvMg=; b=m A+GvMxRXDRiGmrYZyTZYLy5jeiv1lPLTbgVcrVs9jxtwLRoSMs0SBd2MqklGbVuq NB8fx22pcKaFfsuK2CydSr9KT3FEGps/rSlAxLtPpOYNSnjVDPoijCuaifbN0bx8 iAGAUaPFPOXXJaH2fLfZh7b3FrVy6z/ZXS5vqqgVluDG4detlKjMrS3NJZeEAffh 797jEtiloZ/JBx5BNw6MM2Xbp0UnESSPp8TNnbl04vJ21bzb1bXyCM35wgoQrZda p+8WrRRan3+vSrWTjSjLp0cJ9G5OQLwYBB6tb+HO8s+uZEaoXeTE22UOF/0FNTX4 5ksfmq6f/Xd9aNZNK504A== 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= fm1; t=1716334294; x=1716420694; bh=fCKUVZvcRp3AgK4oNHuSew3UHpM3 jQ6yGJ19octUvMg=; b=nnn2GFqzzVT8T1CYiXOy/bHqHW0XBcy/YSkzukQ/oVCt VmvTB938a7bnkKVeuBdR36+RDltJSe/fkMqcrKbW3C3HuQLDXsZd4/dlmR5DEClG q6hf3hZ/9KI0gzD25q7n24ohXllNgV1zfECrrL0yG/ONLkR5rGT26H3F9h1yXUA4 q9Nf4PmGmx+yl2JRElaPe6RX6BPtf5fQk6bwKai0A5PRWa4WTrYWGkM+CuA3yDzx PdMOP8woKaOU8hUZUUKDXeosxESAN9yoRrJmPuNZmcAH6KKUTSAspS3KKamlQo0M koDC/LnflA6Jl0Qy8DOZUdZCzPttV+UjrcZ1/v77RA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeifedgvddvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeejleduueffveevvefgffekjeetvdetfeetjeefvedv udfgiedtuddvgeevveejhfenucffohhmrghinhepghhithhhuhgsrdgtohhmpdhpvggrkh gurdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhho mheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 4B4D11700093; Tue, 21 May 2024 19:31:34 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-480-g515a2f54a-fm-20240515.001-g515a2f54 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <1286eed1-c35e-40ec-a6f0-84926c99c911@app.fastmail.com> In-Reply-To: <99c30f5d-e2f3-4027-a8fc-b3b9c71fb6d1@scriptfusion.com> References: <99c30f5d-e2f3-4027-a8fc-b3b9c71fb6d1@scriptfusion.com> Date: Tue, 21 May 2024 18:31:14 -0500 To: "php internals" Subject: Re: [PHP-DEV] [Discussion] Implicitly backed enums Content-Type: text/plain From: larry@garfieldtech.com ("Larry Garfield") On Tue, May 21, 2024, at 5:48 PM, Bilge wrote: > On 21/05/2024 23:35, Larry Garfield wrote: >> On Tue, May 21, 2024, at 5:30 PM, Bilge wrote: >>> Hi Internals, >>> >>> I struggle to understand the benefit of "basic" enumerations and their >>> diminished API. In particular, I often find myself wanting to use >>> `from()/``tryFrom()` to convert a string to an enumeration. To do this, >>> I must convert it to a "backed" enum and copy & paste each name to its >>> value. In all other regards, I still want it to behave like a "basic" >>> enumeration, so I won't abuse the value of the names to act like a >>> mapping; the values will always mirror the names, and if I need to do >>> any mappings, I'll add match() functions. >>> >>> My question, then, is why can't basic enumerations have these semantics >>> by default? Or, to state it more concretely, what would be the downside >>> to having all "basic" enumerations actually being "backed" enumerations >>> whose values implicitly mirror their names for the purposes of >>> converting to/from strings? Would this not make basic enumeration more >>> useful without any particular downsides? >>> >>> Kind regards, >>> Bilge >> Making enums not be "fancy strings" was a very deliberate decision. The RFC covers that some. There's more information in our comparison research here: >> >> https://github.com/Crell/enum-comparison >> >> And I wrote an article about enum usage a while back here: >> >> https://peakd.com/hive-168588/@crell/on-the-use-of-enums >> >> --Larry Garfield > > Hi Larry, > > Thanks for the resources! Whilst I can appreciate this was a deliberate > design decision that did not come about by accident, I still didn't find > (skimming) anything that directly answers the question: > > >What would be the downside to having all "basic" enumerations actually > being implicitly "backed" enumerations? > > I gather from your (presumably derogatory) referencing of the same as > "fancy strings" that you would not approve such an implementation, but I > am struggling to understand why. > > Cheers, > Bilge > > P.S. Sorry for the (previously) incomplete subject line. "Fancy strings" isn't derogatory. It's an accurate description of how enums work in some languages. (Although usually they're "fancy ints", rather than strings.) The model PHP went with is "fancy objects" (also used by most other languages at this point). From the article I linked: > Enumerations take that a step further by allowing you to define an entirely new type space of values. Just as a Request object is not an integer, neither is a Direction enumeration with values Up and Down an integer. If a function takes an integer as a parameter, and you try to pass it a Request, both the code and the developer are going to just look at you funny. The exact same concept applies if you try to pass a Direction enum to an integer parameter. And also see the "These things are not the same" section. In your case, you want to "upcast" a string to an enum. That means you're doing some sort of deserialization, presumably. In that case, a backed enum is what you want. A unit enum isn't serializable, by design. Now, it's true we didn't try to come up with all the possible shorthands that might make sense. I could see an argument for auto-populating the backing value off the enum name if it's not specified, something like this: enum Options: string { case First; // This implicitly gets "First" case Second = '2nd'; } We left that out of the original RFC to keep things simple and avoid (well, reduce) bikeshedding side quests. However, I think it's a fair discussion to have on whether it makes sense to do that, or similar. I'm not sure if I'd support it myself at the moment (there would also be questions about what happens with an int-backed enum), but I think it's a reasonable discussion to have. --Larry Garfield