Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118089 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 44552 invoked from network); 24 Jun 2022 14:22:06 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Jun 2022 14:22:06 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B300518037E for ; Fri, 24 Jun 2022 09:12:08 -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,FREEMAIL_FROM,HTML_MESSAGE, 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-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) (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 ; Fri, 24 Jun 2022 09:12:08 -0700 (PDT) Received: by mail-yb1-f177.google.com with SMTP id i7so5235625ybe.11 for ; Fri, 24 Jun 2022 09:12:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=bdjHpN1c0ZgEV9dIaFaxgxYBTaKJZWBr5neeZ07xnzw=; b=hp7IjUR7GUieYAYNbVMUtLROdXyZvK2brNvSlhasBvZRsNvqDy/Pnyhm/EBbTK/XLQ 0cCmcrf5zzkf6PlFqJ5vh0jPoPqq/oNE6L00nMFQBu2w6ynkDelI09WMPs6SoZaVFA4f ekeqeGn4D2tyRU03ozScRlN4TV8hVqHPrDujjKyYzk8VoUlJU25j0kinZYF3EOD7H6Ju EzGtKm54hbqhY5PTxuJcbzjFvGc5PXlTcsqF+YSua6KVJ3Ql/IbaoEKs1bVWBlUKhUnB K2Cn3DV/dkhcnZhgNUBqHWVncNzpgotx7jxc8NQVRbrRA2Y4sS/OvVXexN1xTzi0Gx3U WVug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=bdjHpN1c0ZgEV9dIaFaxgxYBTaKJZWBr5neeZ07xnzw=; b=MdodzE9g1K8EanQa6j7O17mRhEzB14gZkLs+BFmmQKa5kXzUW0PToDEoXn/bDEO1qA mUIXlBe8ZOEIQRGR9LpGXwIvpt3cnbKETnDqoRtclFkpZ90NiJ9LOuGZDLfUGxCGoe79 c9xmYsB06UcsMZFtLfQt6ri4P3AUaxifWPLPQGQcU9vrttA0Ff73OfgEvT6TG4pfr9a0 1WIgi0mAMypAgXRpnym2v1dWs1HXwJz6tv+dxXAlBGePN1+MPsEbepCSzA0/uYsQIP5+ UMTyR226m3Z7/iP2lexGHvVSY/JYF0ErlgfI8GgZ04RuVDTKNuFbcSrb5uaKT9yUZBB1 3D5Q== X-Gm-Message-State: AJIora9zOeucAGxMSuCqkvYEWeJl74xi+1kuktoOOvpPj6tyKGdL4xCX dilfpqk+rd4DUlzXsOZI6+EGX6ocK1vd/PpONRQWEx4O2c4znw== X-Google-Smtp-Source: AGRyM1sKHVkLKGOE+Shwa92fllQgA/yjE7l2jSc1+Z/nUClzVdxFK4WKtBvNFUM2HCxkuNVP6M7OhfZOIEpZrtMKGDg= X-Received: by 2002:a25:4109:0:b0:66c:74a1:4ca7 with SMTP id o9-20020a254109000000b0066c74a14ca7mr2939426yba.600.1656087127788; Fri, 24 Jun 2022 09:12:07 -0700 (PDT) MIME-Version: 1.0 References: <9C11261B-B9D0-4342-81EB-60276B3036E5@php.net> <2cee17c0-7b77-4709-94b9-598a4c3d1f49@www.fastmail.com> <616fe4af-4945-82cf-c1e5-aa52535bf1f8@gmail.com> In-Reply-To: <616fe4af-4945-82cf-c1e5-aa52535bf1f8@gmail.com> Date: Fri, 24 Jun 2022 18:11:55 +0200 Message-ID: To: Rowan Tommins Cc: PHP Internals List Content-Type: multipart/alternative; boundary="000000000000f810a605e233d4f5" Subject: Re: [PHP-DEV] [RFC] [Under Discussion] Auto-implement Stringable for string backed enums From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --000000000000f810a605e233d4f5 Content-Type: text/plain; charset="UTF-8" > domain SymfonyPermission: string; > domain AcmePermission: string { 'admin' | 'user' | 'bot' }; > [...] > Domains can also be considered sets, which you could compare directly, > and maybe even calculate intersections, unions, etc: > > The actual values would be ordinary strings, and type constraints would > just be checking the value passed against the domain: > > 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. > Thanks for this idea Rowan, that's really interesting. I would go one step further and require naming the values in the set. Borrowing from the syntax of enums, we could have: set AcmePermission: string { case ADMIN = 'admin'; case USER = 'user'; case BOT = 'bot'; } Then AcmePermission::ADMIN would === 'admin' and, as you said, AcmePermission could be used as a type on functions: function (AcmePermission $perm): AcmePermission { $perm instanceof AcmePermission; // true return $perm; } In order to make this work, we would need to be able to autoload the type AcmePermission.This could be done by the type-checking logic: when checking $var against AcmePermission and the type is undefined, if $var is an int or a string, call the autoloader for 'AcmePermission'. There are other things to consider, like possible changes to reflection, whether such a set also declares a class like enums do, etc.But overall, I really like it and I agree with you: this would provide a really good solution for the problem ppl try to use enums for. I don't know how we could move forward on that idea. By starting another thread? Nicolas --000000000000f810a605e233d4f5--