Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118066 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 67892 invoked from network); 22 Jun 2022 19:54:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Jun 2022 19:54:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2B2B91804D4 for ; Wed, 22 Jun 2022 14:43:52 -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=-0.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (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, 22 Jun 2022 14:43:51 -0700 (PDT) Received: by mail-wr1-f50.google.com with SMTP id r20so4809563wra.1 for ; Wed, 22 Jun 2022 14:43:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=0iuhFI5rcT/4khbCValmjQ+qtwOvJ02CZUO0ruWIzH8=; b=SMFJq93M0ubr5xmdYaocq/38c2nEskQlFZkh70Y2Lg7FqLndP8ZSwIR6fOfzwacSwC U7AnVeOt8XJ7a5Xz+yLIR2an1lrwkWQxL1nz+Z06EdnJ5r1PMznWiefdvqT10UcE8p6w ihwsjOG/8+1V2BSh4EfWlGr0pA60KiW03OInIiLuD4mZCD4uVpDD/mbdr5BmMD/Snryq e9NpgNqMPVjMqxRZOl1dKL8gYweLeC/G4Sn5QhwZrIODD+7CNSL2TFX6+H9oddcN3/3r fIDJm7OEnnGGSbT2XQYC87QcwsVV9peUtBsZGxGoACtYe2bQOT1NW2RtZYuPy6aiwnKu hbCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=0iuhFI5rcT/4khbCValmjQ+qtwOvJ02CZUO0ruWIzH8=; b=Tml8/V29mcTo6ONw3C8aQa98Lb4KctyeQkmKvTDrafqe4p69CN1KlyeilXFXccvk0p KqMJOesA+324S+/oLpRWBvG9q4uDWbqcDVPfZCxCvVO37BvZ7RpOyMMGsJJVpE49xBen /wy1Pgcm9rdiPmvviRkwVF1lVmZ03E94MrrjEHu4U7i5n6KiTOvYvaTVrX+UX8kJL0yw DM3vq/ynTY8KYh3fDs4MzuXAZgV7JkmRmd8PB7mAlO++3IRTEXrjvf4bXy0x9qHwE99j XFWZTBzr9y8T0+DP2XM4O8YNrY/GYgIdGVtHxPvOo1/12mr04CSQxB/O22du+q3qw1ym bLMw== X-Gm-Message-State: AJIora960b9D4fGBL4LfCy5NSWPBZU/HzURykDOxfMO9vj2knBtnYZsM Z44pjLNb2PN4tnUfIcN+4o6ZH5Hpzxg= X-Google-Smtp-Source: AGRyM1s2m0fgleD3kqawUj4adloAj5ZTYocHs+LzJVxxegtVZIXzibdAUTxAeKDpcdX4Kjlsbw0vyw== X-Received: by 2002:a5d:6da3:0:b0:211:3597:62b1 with SMTP id u3-20020a5d6da3000000b00211359762b1mr5236145wrs.660.1655934230443; Wed, 22 Jun 2022 14:43:50 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id k5-20020a05600c1c8500b0039c84c05d88sm834749wms.23.2022.06.22.14.43.49 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Jun 2022 14:43:49 -0700 (PDT) Message-ID: <616fe4af-4945-82cf-c1e5-aa52535bf1f8@gmail.com> Date: Wed, 22 Jun 2022 22:43:46 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Content-Language: en-GB To: internals@lists.php.net References: <9C11261B-B9D0-4342-81EB-60276B3036E5@php.net> <2cee17c0-7b77-4709-94b9-598a4c3d1f49@www.fastmail.com> In-Reply-To: <2cee17c0-7b77-4709-94b9-598a4c3d1f49@www.fastmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] [RFC] [Under Discussion] Auto-implement Stringable for string backed enums From: rowan.collins@gmail.com (Rowan Tommins) On 22/06/2022 18:26, Larry Garfield wrote: > The argument presented is that it's easier to type `AppRoles::Admin` than `"admin"`, because the former provides you with an error if you typo something. That's a valid argument, but... not for using enums. It's an argument for using constants. I wonder if the reality is that neither enums (as implemented) nor constants are the right solution to to this. What users want is some way to say "this value should be a string, but in this context it should be one of this list of strings"; or, sometimes, "is this string one of this list of strings?" Constants can't do that - they give you a way of referring to the possible values, but no tools for enforcing or testing against them. Backed enums can kinda sorta do that with a bit of effort, using ->value and ::tryFrom, but they're not really built for it. A better fit would be some kind of "domain type", which would allow you to write something vaguely like this: domain SymfonyPermission: string; domain AcmePermission: string { 'admin' | 'user' | 'bot' }; assert( in_domain('admin', SymfonyPermission) ); assert( in_domain('admin', AcmePermission) ); assert( in_domain('random stranger', SymfonyPermission) ); assert( ! in_domain('random stranger', AcmePermission) ); Domains can also be considered sets, which you could compare directly, and maybe even calculate intersections, unions, etc: assert( is_subset(AcmePermission, SymfonyPermission) ); The actual values would be ordinary strings, and type constraints would just be checking the value passed against the domain: function doSymfonyThing(SymfonyPermission $permission) {     echo $permission; // no coercion needed, $permission is a string } function doAcmeThing(AcmePermission $permission) {     doSymfonyThing($permission); } doAcmeThing('admin'); // no special syntax needed to "construct" or "look up" an instance Crucially, this solves the described problem of a library accepting an infinite (or perhaps just very wide) set of values, and a consuming app wanting to constrain that set within its own code. It's one disadvantage is the typo-proofing and look up availability that constants give, but you could always combine the two. Regards, -- Rowan Tommins [IMSoP]