Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118075 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 44594 invoked from network); 23 Jun 2022 13:46:23 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Jun 2022 13:46:23 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7876418037E for ; Thu, 23 Jun 2022 08:36:10 -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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 23 Jun 2022 08:36:10 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 2E37E5C010F for ; Thu, 23 Jun 2022 11:36:10 -0400 (EDT) Received: from imap52 ([10.202.2.102]) by compute1.internal (MEProxy); Thu, 23 Jun 2022 11:36:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding: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=fm2; t=1655998570; x=1656084970; bh=Ab/09W2lV57OEXF2BsrrYt4J7 4Hc/G9fbcrz2W+8LYw=; b=KKG3cOdtQC2dtqBx84ob8PphsMT51JchP6L4HgwdC OI950Aann6Emlb/sQzpPqM+NwLsZiFwi7hPJqRxQXTJkFtq/B6Tn9wboU9dlzl6+ yXmORF/U9Dl6Vkh8CqVw4lYtZkV1Op7qoeV7KbQ+a3bGTL+Z4QsdUoc8vjbV5NVw 3+2zYHZZeM7tMb0CrLzx9mheS3uxgOhtGZNCBN1oO1X+EgK0Wn3cdxLRjYN/cfZ2 fckjpNTurjo/D4Oiq//18ehrqhgJx3zFCWZC/hPQWHi9ysUUIFDL48frl3Yj9TtE LxWyHhDFPoCezDFwna7Ai+7UJD7c0ne1knDl6nuKpuohA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding: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=fm2; t=1655998570; x=1656084970; bh=A b/09W2lV57OEXF2BsrrYt4J74Hc/G9fbcrz2W+8LYw=; b=xXuy23pgCUAultrlw L7OKwTNzL7vtH0qiJ0CniAmfl4/Rj0mjrJkDjKroX+UpEtOEM1AE1v3bYrMFSBaM Uox55CUQwC51VyUAibVDaKQf8KuHJ1zremIZpv1cIdLcE/8cs5AxI9zArF070W1n 7fzgM5iYP2W7L6fbnhmgkmqZeT5/izyPcLvahKp6Qm0tsAQJNodl3XpYxeb9Bkyd uYXTf2M3knlQZ9QZZrtXSuKu38iajghEzA8NOq/NAUwzS0dOzu59H09G63gGuPgd 7lM1ypWhbB7g1VTnxk0Jn4PgPbNaugmsZv2fWKRMgkZYIKi+7wVuf8EzEEUaE081 O18Mg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrudefjedgledvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepffffffejffdugfegvedviedttedvgfejffefffej leefjeetveehgefhhfdvgfelnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 0052EC6008B; Thu, 23 Jun 2022 11:36:09 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.7.0-alpha0-713-g1f035dc716-fm-20220617.001-g1f035dc7 Mime-Version: 1.0 Message-ID: <7f5cb2ca-8104-4c46-a4b5-fd15dbc10ff8@www.fastmail.com> In-Reply-To: <616fe4af-4945-82cf-c1e5-aa52535bf1f8@gmail.com> References: <9C11261B-B9D0-4342-81EB-60276B3036E5@php.net> <2cee17c0-7b77-4709-94b9-598a4c3d1f49@www.fastmail.com> <616fe4af-4945-82cf-c1e5-aa52535bf1f8@gmail.com> Date: Thu, 23 Jun 2022 10:35:49 -0500 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] [Under Discussion] Auto-implement Stringable for string backed enums From: larry@garfieldtech.com ("Larry Garfield") On Wed, Jun 22, 2022, at 4:43 PM, Rowan Tommins wrote: > 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 typ= o 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=20 > constants are the right solution to to this. > > What users want is some way to say "this value should be a string, but=20 > in this context it should be one of this list of strings"; or,=20 > sometimes, "is this string one of this list of strings?" Constants can= 't=20 > do that - they give you a way of referring to the possible values, but=20 > no tools for enforcing or testing against them. Backed enums can kinda=20 > sorta do that with a bit of effort, using ->value and ::tryFrom, but=20 > they're not really built for it. > > > A better fit would be some kind of "domain type", which would allow yo= u=20 > 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,=20 > and maybe even calculate intersections, unions, etc: > > assert( is_subset(AcmePermission, SymfonyPermission) ); > > > The actual values would be ordinary strings, and type constraints woul= d=20 > just be checking the value passed against the domain: > > function doSymfonyThing(SymfonyPermission $permission) { > =C2=A0=C2=A0=C2=A0 echo $permission; // no coercion needed, $permissi= on is a string > } > > function doAcmeThing(AcmePermission $permission) { > =C2=A0=C2=A0=C2=A0 doSymfonyThing($permission); > } > > doAcmeThing('admin'); // no special syntax needed to "construct" or=20 > "look up" an instance > > > Crucially, this solves the described problem of a library accepting an=20 > infinite (or perhaps just very wide) set of values, and a consuming ap= p=20 > wanting to constrain that set within its own code. > > It's one disadvantage is the typo-proofing and look up availability th= at=20 > constants give, but you could always combine the two. Interesting concept. I'm not sure if I like it yet, but it's interestin= g. :-) It somehow feels related to Go's type aliasing, but I'm not sure= if that's a fair comparison. Is there a type-theoretic basis we could look at for that? It seems lik= e the sort of thing some mathematician has likely thought through for fu= nsies before. --Larry Garfield