Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119500 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 94721 invoked from network); 9 Feb 2023 06:11:51 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Feb 2023 06:11:51 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8E370180341 for ; Wed, 8 Feb 2023 22:11:50 -0800 (PST) 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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS29838 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout2-smtp.messagingengine.com (wout2-smtp.messagingengine.com [64.147.123.25]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 8 Feb 2023 22:11:49 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id BE42332008FD for ; Thu, 9 Feb 2023 01:11:48 -0500 (EST) Received: from imap50 ([10.202.2.100]) by compute4.internal (MEProxy); Thu, 09 Feb 2023 01:11:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1675923108; x= 1676009508; bh=4lJhV07QgL+dUse5yH9eSQJKKj6nRqUa45m5AhEXhNo=; b=i zbKjrHUxr5Bw28616ErMvIOI/G6UTGyR5jgF9UndNYuU4SSIcaG/qAl5r3caTxKv JKaCHmAe/Ci5orGC+UAtzv9d9Ow3OJg+zf2ZE6+69vFhGJByJeLTofiB1skkpVCR WvwjjhFfm0LPIvrCLpKNTg6S8L75+OfRfMJ7TcpneL8+jyCgC8f174FQVY+I8Jck 5KMVRZUxGE389lIg1VkZsj9YagsFGfJERnvN41uBowHD/NLY/7hRZtJfQ5vg5nRd qgVRl68/tvv0XZGWivbNUX39RgLgTBqVfaACFXey/JlgeHYL0wCOJpTUVWM5/qB1 qyMSNq3qN1nAeF6hkAFfQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1675923108; x=1676009508; bh=4lJhV07QgL+dUse5yH9eSQJKKj6n RqUa45m5AhEXhNo=; b=tZjZvM0Q7r8O5bSGPLv/JMjqF6EeLSy//UO8i5u81c0s sTL+Epi/LSRNk80Z6cn3QyZ4I59NlX4mNxjFpAI6qBnja3o748+YUPGDXi11LfK6 hrWaNZDp+kZDaJKbqtgiLqxjB+t6FU/8cRD04PJklPM6NR8QzH6TYJZsEbHm7VI3 gezBowrk41WPvvRoNBhsw51ea4xaPaDtRvJU3dT5aMnMN1nYIHpWUlCXA37Bfb17 3UYRSj7MDJy7yRaaKfICtvVypZRunhgyR20O8SUBKkcGGPm6Uhkeaj3qUnqu+gQu 84fdYwVF04C7JZmC77o3jXPHxHt8MMFjqZlMJDT1ww== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudehvddgfeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeglefgkeduiedvvdetffeujefftdfhjeeiveehgfff keduveektddvledvvdfffeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 204591700089; Thu, 9 Feb 2023 01:11:48 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-156-g081acc5ed5-fm-20230206.001-g081acc5e Mime-Version: 1.0 Message-ID: In-Reply-To: References: Date: Wed, 08 Feb 2023 22:11:27 -0800 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] RFC proposal: values getter in BackedEnum From: larry@garfieldtech.com ("Larry Garfield") On Wed, Feb 8, 2023, at 5:30 PM, Sergii Shymko wrote: > Hi, > > I'd like to propose an improvement to backed enumerations introduced in > PHP 8.1. > When using enums, it's very common to get all values using the > following boilerplate: > $enumValues = array_map( > fn (\BackedEnum $case) => $case->value, > ExampleEnum::cases() > ); > > The primary use case is when an enum is used in a "set" (array of values). > For example, a user inputs a subset of available values. > The input may be optional defaulting to all values if omitted. > For example, an array option of a CLI tool: > $inputValues = $input->getOption('enum') ?: $enumValues > > The secondary use case is for informational purposes. > For example, the CLI tool may list all available values of an enum > option in --help. > The list of values can also be used to produce informative error > messages: > $output->writeln("Option is invalid, allowed values: " . implode(', ', > $enumValues)) > > The last use case is input validation. > Although tryFrom() can be used, having access to all values can come in > handy. > For instance, you could immediately get valid and invalid enum values: > $invalidValues = array_diff($inputValues, $enumValues); > The implementation would be more verbose and wasteful otherwise: > $invalidValues = array_filter($inputValues, fn ($value) => > !ExampleEnum::tryFrom($value)); > > The proposal is to add the values getter to backed enums: > interface BackedEnum extends UnitEnum > { > public static values(): array; > } > $enumValues = ExampleEnum::values(); > > Indeed, it's possible to implement this userland in a trait reused across enums. > However, the implementation in C will be substantially more performant. > For instance, the values() method call can resolve to a constant! > > An alternative syntax is a pseudo-constant, for example, > ExampleEnum::values. > Unlike the constant, the method would be part of the interface > complementing cases(). > Method cases() itself is not a constant only because it returns > instances. > I'm fine with either syntax leaning towards the method for consistency. > > Please let me know your thoughts. > With enough support, I'd be happy to submit an RFC and implement it myself. > Some guidance on the performance optimization aspect may be necessary though. > I'm still missing the karma necessary to create RFCs in the wiki. > > Regards, > Sergii Shymko I would be OK with this, assuming the method approach. (The class constant feels clunkier.) --Larry Garfield