Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123410 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 964761A009C for ; Thu, 23 May 2024 10:38:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1716460767; bh=6EwFHMM4eQ6Q88WiVw0CmixvPBPdV0e7sSJSGcE227U=; h=Date:Subject:To:References:From:In-Reply-To:From; b=gqBjr36WBXStHnFnI2U6WJXoRCywZHwPUkPsYRBK/xh3v8Q5N0gQnUVZR4xwNAViA JGtbe7qk8fCAlALIjg2AQ9U4HMSoRi5ro87s6AUdrxgKKEzyu+rd8F1NlumwyGbTXj snB+juP8BpkDTBtu6HjpM4m7VAIBNvGArd4enXphUTwLkl7z+5J0onSkN8rmjjJalA YySSygpOGDS5cxaUqJoPveGDrZy/eiWFJdl8i6NUFbrTcTePJJEQmTL7W+rWy89BtH vr98W7kTlxKZLUvt2rprgX8nTGxXTNr/AzSqJw2WSmvOTJKGBB/6JoxFWicjrErIYi 2OONDfADUQx9Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 216BE18003B for ; Thu, 23 May 2024 10:39:26 +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,SPF_HELO_PASS,SPF_PASS,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 wfout1-smtp.messagingengine.com (wfout1-smtp.messagingengine.com [64.147.123.144]) (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 ; Thu, 23 May 2024 10:39:25 +0000 (UTC) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfout.west.internal (Postfix) with ESMTP id A417E1C001F9 for ; Thu, 23 May 2024 06:38:27 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Thu, 23 May 2024 06:38:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; 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=1716460707; x=1716547107; bh=jKFBn+z/2F cF3Sobtg3OBh6Hnc9/yjjvcevCEH7WjNE=; b=iRPKJqVa2GtXmS0xFhDTHBMqpg OVt8U1a9mCas8pxXsWzoaTYSMS0wELz7b61+5vZcd/jRuuY+2oaZrKjvZ9B2Rs2P F8nEs/HyjGQ4vkVw/ujBmOvi1itCsGE09+HbCKu1kesHZiSBsj15tAy8c0v5Ptqi IEh7HQbJO8R03ol1tzSzGpKVBZvi7MjV+UQb1O9aqbWVQNZIHlgZdcmU3hbZwMVw 3PVwLDurSvRKQj94uoZFqLlT225wxWiEjdTV8lnRKbYNNr3FZYgDjThBYqABa7Jh 3oRErU9P3faLNDd7ENBNmbDe/dXO1ySDgixzww5glAZxQQ0mO2OjgqB2JUUA== 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=1716460707; x=1716547107; bh=jKFBn+z/2FcF3Sobtg3OBh6Hnc9/ yjjvcevCEH7WjNE=; b=ie+zB/Km4YHQrVDq8y3xGENTPee+ofm1mDAyZceLvgEa HUMjA4nxQZyxZfwyNlOCBV6FmcCwvKu/S66leWiXpNvSRGfXMRvTmBP0KE11OgnQ wQ8tHd64R8II7HPFoF3jmp/RpCatT4GZEuuMDjWx0ci22l96xkxjt98FyaSV66H1 fcdzRakVJq1FwRP4eHg3CASr6Xzk3Y+VsXnzBE+ZcADi6oHMRsB8nIKlszQxnItR l8CfjH66CTtywik8tIgCL40po5/tXm5ZbvvDVJFWNZrUsavfIm2HIg1/RCQFmZZk oI4rRagOIDCF6t5ABhvkdu06o4CXs2JYcEU4N1ECcw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeiiedgfedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpegtkfffgggfuffvfhfhjgesrgdtre ertddvjeenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhsucglkffoufhorfgnfdcu oehimhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecuggftrfgrthhtvghrnhephe etleeiiefgueduieeuieffvdevheduueefkeejuefgffeftdeitdegtedtleetnecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepihhmshhophdrph hhphesrhifvggtrdgtohdruhhk X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 23 May 2024 06:38:26 -0400 (EDT) Content-Type: multipart/alternative; boundary="------------sKnfNo4LG6aFTG9v5OyabMq3" Message-ID: <87a6de60-cf6e-471b-ab6d-dee005da622e@rwec.co.uk> Date: Thu, 23 May 2024 11:38:21 +0100 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [Discussion] Implicitly backed enums To: internals@lists.php.net References: <99c30f5d-e2f3-4027-a8fc-b3b9c71fb6d1@scriptfusion.com> <1286eed1-c35e-40ec-a6f0-84926c99c911@app.fastmail.com> Content-Language: en-GB In-Reply-To: <1286eed1-c35e-40ec-a6f0-84926c99c911@app.fastmail.com> From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") This is a multi-part message in MIME format. --------------sKnfNo4LG6aFTG9v5OyabMq3 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 22/05/2024 00:31, Larry Garfield wrote: > 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'; > } This reminds me of the short-hand key-value syntax that JavaScript allows, and people have occasionally requested equivalents for in PHP, where { foo } is equivalent to { 'foo': foo } The downside I see to all such short-hands is that they make it much harder to refactor safely, because the identifier and the string value are tied together. For instance, maybe you want to rename Options::First to Options::Legacy and Options::Second to Options::Modern so you edit the enum, and find all references in code: enum Options: string { case Legacy; case Modern = '2nd'; } But now everywhere you've serialized the old value of "First" is going to break, because the first case now has the implicit backing value of "Legacy" instead! To avoid this, you have to go ahead and specify all the backing values: enum Options: string { case Legacy = 'First'; case Modern = '2nd'; } Having to specify both the name and value in the first place makes that decision much more obvious, for what seems to me to be very little up-front cost. Regards, -- Rowan Tommins [IMSoP] --------------sKnfNo4LG6aFTG9v5OyabMq3 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit
On 22/05/2024 00:31, Larry Garfield wrote:
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';
}


This reminds me of the short-hand key-value syntax that JavaScript allows, and people have occasionally requested equivalents for in PHP, where { foo } is equivalent to { 'foo': foo }

The downside I see to all such short-hands is that they make it much harder to refactor safely, because the identifier and the string value are tied together.

For instance, maybe you want to rename Options::First to Options::Legacy and Options::Second to Options::Modern so you edit the enum, and find all references in code:

enum Options: string {
  case Legacy;
  case Modern = '2nd';
}

But now everywhere you've serialized the old value of "First" is going to break, because the first case now has the implicit backing value of "Legacy" instead!

To avoid this, you have to go ahead and specify all the backing values:

enum Options: string {
  case Legacy = 'First';
  case Modern = '2nd';
}

Having to specify both the name and value in the first place makes that decision much more obvious, for what seems to me to be very little up-front cost.

Regards,

-- 
Rowan Tommins
[IMSoP]
--------------sKnfNo4LG6aFTG9v5OyabMq3--