Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123422 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 489DB1A009C for ; Thu, 23 May 2024 18:48:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1716490182; bh=A5247M88b2kawkmVx6/ikgROQG3MPVNklkfGqI7U000=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=kuypy3fwZbvlZQlTF0L/SmBpamOwYQebOplCzHdV42KV+aw+QFIkHRZLPzwBxCtS/ 3r0ni/E4Sg7Im2cs7KrLTb7coSoR1pEF4Bwc/3nLUo4lnBOY8KsmYRBfPNx/KrjEjj IC543Fqsq0zS2aU53VRM0A96gKz33jTMSidnjHXEzzKsrF58I/9KR7VgKIk+I5ksRQ mxD4/OHS2AzRSGiViY4HCvOI1NfpW8ZJzkxia8KDlmAaLTzuLBtSN+b5ekZBCqFLDW MD7nyoum+CShFV1/wR5QfulYFRUFRHitdcsJw0sVRhR9OTnPB9iAPBx4rsI0J8KWWL Yq/txKTC4xRGw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 749441805A1 for ; Thu, 23 May 2024 18:49:41 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_NONE, 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 chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 23 May 2024 18:49:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1716490122; bh=9zautQihB7dVAHRcTMBAR1uvAFvJya2Cg2ptCGlVKgk=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=UpSmxYPMCV7tLayPQ0co8lRnc4fFN0QUeT4WSEmMBIxI4zMjxVulRHpxcuuGg4glS QHJLYdXCvxlPQuTHQZI3VCuhH1ucK0LA22dNwApZICksChnyK9mMTJrSXxwfAC0/Mv 9HZi3xjHEo28HO1HJXuf3j5cuXpuNBjeJOYa00A9lP+yjIiRvrTw6xMTXCiPc+3N5S DxteMVVJfGIZctiDSfAp2Z/SDb3MBzvQVyguUt/G5O13Y8BTE7MYVTEZhBrIALYCZM F7CifqhU7pz/BOYdoLoG9IWjngYI9AJBl+2cQNKTSwiwxIO5KCv+yV41Pkw4pOdiSp ir+nJLB/lZTcw== Message-ID: <0931b1f7-bd99-4468-b3b7-a4beca395368@bastelstu.be> Date: Thu, 23 May 2024 20:48:41 +0200 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Subject: Re: [PHP-DEV] [Discussion] Implicitly backed enums To: Aaron Piotrowski Cc: PHP Internals References: <99c30f5d-e2f3-4027-a8fc-b3b9c71fb6d1@scriptfusion.com> <1286eed1-c35e-40ec-a6f0-84926c99c911@app.fastmail.com> <3cac0454-ceb2-449a-8b9a-1fac3f866a9c@scriptfusion.com> <8D146901-ADF1-4BAA-B6AA-2CCCFE7A0158@trowski.com> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi On 5/23/24 20:34, Aaron Piotrowski wrote: > This solution is flawed. Not every constant is necessary an enum case. It also isn't type-safe or as nice to read as tryFrom() and from() static methods. > Having regular class constants on an enum is somewhat questionable [1], but fair enough. In that case an `instanceof ExampleEnum` check will do the trick. But you don't need to iterate on all the enum cases, checking them in sequence, like in your suggestion. As for the initial suggestion of implicitly backing enums: I'm strongly against that. Personally I find that backed enums are almost never useful. Serialization is more reliably performed using the case name and often there is more than one reasonable scalar representation and then you need regular methods to obtain the different representations. Singling out one of those scalar representations as the “blessed” representation to store in the backed value feels semantically incorrect to me. Best regards Tim Düsterhus [1] Except possibly to alias an enum case for BC purposes.