Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128637 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id BB2D31A00BC for ; Thu, 4 Sep 2025 14:51:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1756997377; bh=q7mmPeWZhPEtWhwe9cBzhQ5RreslYwlGy6jZz89muFA=; h=Date:From:To:In-Reply-To:References:Subject:From; b=EgAhgj/2xxyqIVT3VZLGDoqnqgLWCcEBPer1wNUq2SQYuubHDE22p+Jg0OHIUADT5 q7PzerEN8FCFNmHOF3QZ+8EtqRnOVrBtBbnQPnDrzaJ6wgt21qR8mBxhHHvE3cJuoq CV+js39mnJ57loAPk74A+ppen+0Hv3sv7ChwoZkL8PcR1v2qUZJsuZoWcwLjsditCL zncUHR/cW+vVsj3HdturpKrldYoCQSXqF9wNdAEQuzExLyKgobp1jtXEW9zDc5PwEA 4NAiigvUsKvViuARHHsyMN+UQILZLbwqt4sn8dcT+N9G0ubULz89dMSXlDerXOtZrA FfguTRm5zOVOA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 55272180087 for ; Thu, 4 Sep 2025 14:49:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fout-a7-smtp.messagingengine.com (fout-a7-smtp.messagingengine.com [103.168.172.150]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 4 Sep 2025 14:49:32 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id 46B6BEC029B for ; Thu, 4 Sep 2025 10:51:01 -0400 (EDT) Received: from phl-imap-05 ([10.202.2.95]) by phl-compute-05.internal (MEProxy); Thu, 04 Sep 2025 10:51:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1756997461; x=1757083861; bh=MJjUcR8fFM nrh1HMCQVvDK/GNn9SNSOZAnxCNMD4FXc=; b=mb6imV0/D9Vnb/8c8SQa5/vVto ZXis19gbPDwrOvj+7kJ3jpus/kY5+2l6ViAfsVH6pYWpbnGCrRbR4ec/O/ycSyW+ URXcKBLeNQTI6UPNmOHZSxplzqrNGFIVdOtl/yaOV6HLx+1qSJOfZWmL5wNhRySi ahvOnvD9qK6Tah6Y72uMff71pzBk7niI/iXmL8CP/mJYWvnKsAqYC2zaT5/qu3O1 BaGo7hYWsUNnMuHtuvYCrnPEruaz49e3RO528saBzMTWAgE7Y1ocozYbUd5rzQeN ttUm6TD1YOnJQBFYF5DSjcreb/iD19rj8YtZF34Gtvw2JWBlE7wJ/cCdI+kQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1756997461; x=1757083861; bh=MJjUcR8fFMnrh1HMCQVvDK/GNn9SNSOZAnx CNMD4FXc=; b=OWlyUHsCOsVhBkHy9xD3FyOalIAZJ9duu/d3MDempVSVGjt098i qLb965iOIJjBL3rdbLtQQsDpKq6aSSa2XXMhnvOjGWylXeP+wRa+JnEfJjGYyoa+ 7xvP/rDP2TxLMJVpt7HTtbxnqw8puW0qr5qYSSuBFPeICxYyp0ANURfsyi4oOdpa Rg0cz+p3BbUFLvv1XYK+hvmi2gcAieczLbq0VfPhGGKqw+/dw+OtmcCK/LBrg+AJ WksGk+B3dtvKjnSdQEcVytSLdvqEkoHQOcAntivv1dFfhIeBHbLlkiUmJBwKfeDH E65K7tbQqO6NKsZHK9/VG4+T5taQx7f/5DQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdeifedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceurghi lhhouhhtmecufedttdenucenucfjughrpefoggffhffvkfgjfhfutgesrgdtreerredtje enucfhrhhomhepfdftohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurdgt ohguvghsqeenucggtffrrghtthgvrhhnpedtueejtdethfeulefhtdelieduteelffdtud elheffgedtieehhfelieejgfevgeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgr mhepmhgrihhlfhhrohhmpehrohgssegsohhtthhlvggurdgtohguvghspdhnsggprhgtph htthhopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhs sehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id B15E61820082; Thu, 4 Sep 2025 10:51:00 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: AFxHlD4AvJc3 Date: Thu, 04 Sep 2025 16:50:08 +0200 To: internals@lists.php.net Message-ID: In-Reply-To: References: <9c875e83-ddc8-4c5c-a368-747bef46e4a2@app.fastmail.com> <25e79d21-7b7f-4d7f-b5cb-1e9cfd0aa657@varteg.nz> Subject: Re: [PHP-DEV] enum flag backed enum Content-Type: multipart/alternative; boundary=efcfb98505114f1ea4a2023034f71917 From: rob@bottled.codes ("Rob Landers") --efcfb98505114f1ea4a2023034f71917 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Thu, Sep 4, 2025, at 15:56, Larry Garfield wrote: > On Wed, Sep 3, 2025, at 7:20 PM, Morgan wrote: > > On 2025-09-04 09:23, Marc B. wrote: > >>> > >>> Attempting to use a backed value that is not a power-of-two would = result in a compilation error. Bitwise operations are performed as thoug= h they=E2=80=99re performed on a regular integer. > >>> > >>> What do you think? > >>=20 > >> I think what we actually would need is an EnumSet which can be used= with any enum but (based on its ordinal) but this would play nicely tog= ether with generics `new EnumSet`. > >>=20 > > > > Agreed; using enums-backed-by-powers-of-two is just a way to encode = sets=20 > > of values drawn from an enum (it's a way to enumerate the possible=20 > > subsets of a set). It should be possible to have a set of Enum value= s=20 > > for any Enum, rather than break the Enum type concept by exposing=20 > > implementation details, and allow sets for some types but not others=20 > > depending on how they are serialised. > > > > A class can be built in userspace as a "SetOfEnum"; or such a class=20 > > could be added to the standard library; or even have, for each Enum=20 > > declared, an EnumSet (final) class is also implicitly declared. >=20 > It feels like there's two different related topics here. >=20 > One is using enums as a way to define a set of bit flags rather than c= onstants. I'm not sure if this is necessary, but if so, I'd expect a bi= t more automation than the initial proposal here. The statement that bi= twise operations would fall back to integer ops makes me think it falls = into the "fancy constants" category we have tried to avoid. >=20 > The other is enum sets, which I am very much in favor of but designing= those in a clean way is non-trivial. My collections research with Deri= ck a while back included a `Set` type, which also supported operator ove= rrides for the |, <, <=3D, -, and other operators, so it did effectively= give a very nice enum set syntax. Whether that is the optimal approach= or not is unclear. But an Enum Set feature should work with any enum. = (And of course runs into the eternal generics question.) >=20 > So, Rob, which one are you really talking about? >=20 > --Larry Garfield >=20 Hey Larry, Marc, Morgan, You are all asking basically the same question; I think: "This looks lik= e a leaky abstraction and could possibly be solved more elegantly?" I think this is a fair observation and a fair question; but I think it i= s important not to have "magic". The power-of-two rule is to make it pos= sible to work back how $enum->value =3D=3D=3D 15 (0x1111) even if you ar= e completely new to the language. If you just use some magical cardinal = order, it is impossible to reserve ranges, handle communications with ex= ternal systems, etc. I'm not opposed to it, however, especially when end= -to-end values are in PHP itself. But, when you need to communicate with= external systems, being able to have that control is *very* important. After doing a bit more digging, it appears this can be handled entirely = as an extension, so I think I'll just go that route for now. =E2=80=94 Rob --efcfb98505114f1ea4a2023034f71917 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Thu, Sep = 4, 2025, at 15:56, Larry Garfield wrote:
On Wed, Sep 3, 2025, at 7:20 PM, Morgan wrote:=
> On 2025-09-04 09:23, Marc B. wrote:
>>&g= t;
>>> Attempting to use a backed value that is not a= power-of-two would result in a compilation error. Bitwise operations ar= e performed as though they=E2=80=99re performed on a regular integer.
>>>
>>> What do you think?
>> 
>> I think what we actually would need i= s an EnumSet which can be used with any enum but (based on its ordinal) = but this would play nicely together with generics `new EnumSet<JsonOp= tion>`.
>> 
>
> Agreed= ; using enums-backed-by-powers-of-two is just a way to encode sets =
> of values drawn from an enum (it's a way to enumerate th= e possible 
> subsets of a set). It should be possible= to have a set of Enum values 
> for any Enum, rather = than break the Enum type concept by exposing 
> implem= entation details, and allow sets for some types but not others 
> depending on how they are serialised.
>
> A class can be built in userspace as a "SetOfEnum"; or such a cl= ass 
> could be added to the standard library; or even= have, for each Enum 
> declared, an EnumSet (final) c= lass is also implicitly declared.

It feels like= there's two different related topics here.

One= is using enums as a way to define a set of bit flags rather than consta= nts.  I'm not sure if this is necessary, but if so, I'd expect a bi= t more automation than the initial proposal here.  The statement th= at bitwise operations would fall back to integer ops makes me think it f= alls into the "fancy constants" category we have tried to avoid.

The other is enum sets, which I am very much in favor = of but designing those in a clean way is non-trivial.  My collectio= ns research with Derick a while back included a `Set` type, which also s= upported operator overrides for the |, <, <=3D, -, and other opera= tors, so it did effectively give a very nice enum set syntax.  Whet= her that is the optimal approach or not is unclear.  But an Enum Se= t feature should work with any enum.  (And of course runs into the = eternal generics question.)

So, Rob, which one = are you really talking about?

--Larry Garfield<= /div>


Hey Larry, Marc, Mo= rgan,

You are all asking basically the same que= stion; I think: "This looks like a leaky abstraction and could possibly = be solved more elegantly?"

I think this is a fa= ir observation and a fair question; but I think it is important not to h= ave "magic". The power-of-two rule is to make it possible to work back h= ow $enum->value =3D=3D=3D 15 (0x1111) even if you are completely new = to the language. If you just use some magical cardinal order, it is impo= ssible to reserve ranges, handle communications with external systems, e= tc. I'm not opposed to it, however, especially when end-to-end values ar= e in PHP itself. But, when you need to communicate with external systems= , being able to have that control is very important.

After doing a bit more digging, it appears this ca= n be handled entirely as an extension, so I think I'll just go that rout= e for now.

=E2=80=94 Rob --efcfb98505114f1ea4a2023034f71917--