Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119777 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 68875 invoked from network); 29 Mar 2023 15:56:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Mar 2023 15:56:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 773021804AC for ; Wed, 29 Mar 2023 08:56:14 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 176.9.0.0/16 X-Spam-Virus: No 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 ; Wed, 29 Mar 2023 08:56:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1680105372; bh=lS4D8cUquTU2TCgd6jVz4agD3mUbrRVDJic/+WQSZAc=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=KqMURMGllTN+kzLbx4bVYufuyKb03pTIxSbasSv4eFnje4DHe+FO3pWlv/Sjgbnz5 XM2+paoqSrKMRxx/ywwNLDb7oigM0jqXWWHwA2istlLBVsbIvKuSf9rD+/8pgTkJkx lqhLLloPkfMFhRc/AXAmf/zGLK4Bv3beqa0n9mPJCDwL8Uu7fi4AhMFANwapyJ2Fai ZA6qXncnsOyjmrl/Q/4qMdiJn/5SPlio/KSS3bAHSxpErIHkgOc2dR20A6S23n6WEY biwHKl4ril141W9t21KHyZ1OBgSxDWdNLC9I+ev9UaT8dubeSUfuvTtzyBd9S3JkG3 pYKjq4tKmk52g== Message-ID: <1414702b-1f07-2a67-284a-6ba604f8b5e0@bastelstu.be> Date: Wed, 29 Mar 2023 17:56:11 +0200 MIME-Version: 1.0 Content-Language: en-US To: internals@lists.php.net References: <8bbe91fa-9f32-e2c6-91b9-e982d25042ed@bastelstu.be> <09A79162-3FB0-4443-9654-CCECE0B9865A@cschneid.com> In-Reply-To: <09A79162-3FB0-4443-9654-CCECE0B9865A@cschneid.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] [IDEA] allow extending enum From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=c3=bcsterhus?=) Hi On 3/29/23 12:41, Christian Schneider wrote: >> And to give a specific example from PHP 8.3: As part of the Randomizer additions RFC (https://wiki.php.net/rfc/randomizer_additions), PHP 8.3 got its first "natively included" enum (\Random\IntervalBoundary). This enum works together with the new Randomizer::getFloat() method to specify if the given min and max value may be returned or not. >> >> The Randomizer::getFloat() method internally includes switch statement that chooses the implementation based on the IntervalBoundary value given. >> >> If a user would be able to extend the IntervalBoundary enum, the method would not be able to make sense of it. >> >> The IntervalBoundary enum completely enumerates all four possible combinations for the two possible states for each of the two boundaries (2*2 = 4). By definition there is no other valid value. > > I do understand the reason behind making Enums final and your example illustrates the point very well. > > I still have a question I already asked in a different context: If there ever is the need to add a case to an Enum, was there any thought put into making this possible? Or is this categorically ruled out when using Enums? Adding a case to an enum would certainly be considered a breaking change for folks that rely on having exhaustively handled all possible cases. > Let’s look at a very, very hypothetical example: Imagine a Randomizer is much, much slower for certain boundaries and it is decided that some programs do not care about Closed/Open but instead care more about speed. So they would want to use something like IntervalBoundary::Fastest. In this specific hypothetical example, the fastest case would be one of the existing 4 cases - an alias if you want to call it that. Defining an alias is possible without breaking compatibility by leveraging class constants: https://3v4l.org/tT0Ho Alternatively a method "public static function getFastest(): self" would be possible. > As far as I understand such an addition would *never* be possible, right? This means people defining Enums have to be very very certain that no one will ever want another value, right? > I believe the short answer to the last question is "Yes". Best regards Tim Düsterhus