Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128616 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 5ED7F1A00BC for ; Wed, 3 Sep 2025 20:45:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1756932241; bh=uWdmKiQiO2GGcp9+zZex/jXO8dmgt6Hoojo/EblfGcI=; h=Date:From:To:Subject:From; b=Acc1N5AixNLdkWi9bcPocQyK9M79brnV/1Z0dARx4X9JAxL387v2OUG7cbaBWo9St pOzn3T91FL5oS2f6iJxwjDKNoEmP7YIihw7/SAA2wQcOfjpIfK9Zpb4n2O7iS/13mF cXJArqSer5O1/FuCVmm23qdnWU+tciLEJ1f/Yr9HTDT75A54sU1azFR7fg68rUVSrH M5qbFwkohFor8BhfzwbQzBtSkeXuI80N/TTi7Wdg9sO1M5N9BeARzPMZtx5GroRwQM 45rl19oG7b5SU0ZRDFP4Eztv7oKoM8teBxv7cClhsbdE5KVHnLjjtKBn5hklzQMDzC V1HDV8w+ncXng== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A59B41801D6 for ; Wed, 3 Sep 2025 20:44:00 +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=-2.8 required=5.0 tests=BAYES_00,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 fhigh-a3-smtp.messagingengine.com (fhigh-a3-smtp.messagingengine.com [103.168.172.154]) (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 ; Wed, 3 Sep 2025 20:43:50 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfhigh.phl.internal (Postfix) with ESMTP id 4E657140047C for ; Wed, 3 Sep 2025 16:45:19 -0400 (EDT) Received: from phl-imap-05 ([10.202.2.95]) by phl-compute-05.internal (MEProxy); Wed, 03 Sep 2025 16:45:19 -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 :message-id:mime-version:reply-to:subject:subject:to:to; s=fm2; t=1756932319; x=1757018719; bh=jvZcSU2JHeT6BTyJMZMaFMumN+aFod1k 0b81DgK5Tx8=; b=OBan9t6T6HzHSj4hWiEZ2/PVvuQ3EPSDCrZWw0elY/lXpToF ifqOzMJH5VtOXhbGmDjDutVVQhKjANDLAeEQYPRzb8Ir7mcS0FXSyULXiKq8ahZy WMO0BLROb3lS5Crz24kOdh2T9TBx2/xzt+zeuGKboV92aryptiNGj4cTdcvOtdd8 figxomSzLo2V5Drr4e5dm2xMAsky9JxWBh/PTQoNPOrLVRsZz1AhJDIUHqPb9Fhf zKWLtFZiql62P1Eo7AOnDYDKPyqC+xwhwhS1Wi5NDzSY5/zx8pyP+dDyfdXJ1f7O sUCQGnEgmPdvtuDMQ0IGiTpv4Z9biJMlwShreg== 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:message-id :mime-version:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1756932319; x= 1757018719; bh=jvZcSU2JHeT6BTyJMZMaFMumN+aFod1k0b81DgK5Tx8=; b=H CZoIW+gXOw+cClOQPlo0D+QAf3mzqCftdVsttf1A7FD1KZ/MD/V+8aB/qNJhOHSh eJpGtu6hEnKQ0lI117qSqOZpZrd7U13gOMAVwFQYaOEXCoh+z3cggtNBesEON3Jx KIcZ4mfXuUTXualpX6ExriSJBVDz4WrXEeh7O9h/vC7h8Qz/wNYF9FLb6UahtB+L uWV0KimMPhkKYf90O4UriT9zAD8je1NjLFD5SoaS0NFsPdoKHHM/dYy/Yqbl1XPr ethV3ppI9PnBPCIrsGYvcjnSrYgAQXsOCxTvFoZ9Wl2WmjLmctmE0BbC2YC4oWBO Z3JyXaMtLrhQWjNL/H58Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdegudegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceurghi lhhouhhtmecufedttdenucenucfjughrpefoggffhffvkffutgesrgdtreerredtjeenuc fhrhhomhepfdftohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurdgtohgu vghsqeenucggtffrrghtthgvrhhnpeehgeetffdvjeethefhjedtffeihfehffevhfduff evkeehvdffleehuedvveegffenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhep mhgrihhlfhhrohhmpehrohgssegsohhtthhlvggurdgtohguvghspdhnsggprhgtphhtth hopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhssehl ihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id DE565182007A; Wed, 3 Sep 2025 16:45:18 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Wed, 03 Sep 2025 22:44:58 +0200 To: internals@lists.php.net Message-ID: <9c875e83-ddc8-4c5c-a368-747bef46e4a2@app.fastmail.com> Subject: [PHP-DEV] enum flag backed enum Content-Type: multipart/alternative; boundary=4ee583bdff6c4bd88a5d3247da9bdbee From: rob@bottled.codes ("Rob Landers") --4ee583bdff6c4bd88a5d3247da9bdbee Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello internals, I would like to propose a new type of backed enum: flag. This is an inte= ger-backed enum that requires power-of-two values that can be combined w= ith the bitwise operators to be used as flags. In some core functions, it might look like this: enum JSON_FLAG: flag { case None =3D 0; case ThrowOnError =3D 0x1 << 1; // etc. case PrettyPrint =3D 0x01 << 4; // etc. } function json_decode(string $data, bool $assoc =3D false, int $depth =3D= 512, JSON_FLAG|int $options =3D JSON_FLAG::None) {} json_decode($data, options: JSON_FLAG::ThrowOnError | JSON_FLAG::PrettyP= rint); Note that the goal here isn=E2=80=99t so much to save typing space, as i= t is to provide easier discoverability through IDEs, better type safety,= and better ergonomics in the language overall. Attempting to use a backed value that is not a power-of-two would result= in a compilation error. Bitwise operations are performed as though they= =E2=80=99re performed on a regular integer. What do you think? I couldn=E2=80=99t find any similar proposals (listed on the RFC page) b= efore proposing this on the list, so if you=E2=80=99re working on this, = please speak up; I=E2=80=99d like to join forces. =E2=80=94 Rob --4ee583bdff6c4bd88a5d3247da9bdbee Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Hello intern= als,

I would like to propose a new type of back= ed enum: flag. This is an integer-backed enum that requires power-of-two= values that can be combined with the bitwise operators to be used as fl= ags.

In some core functions, it might look like= this:

enum JSON_FLAG: flag {
  = case None =3D 0;
  case ThrowOnError =3D 0x1 << 1;<= /div>
  // etc.
  case PrettyPrint =3D 0x01 <= < 4;
  // etc.
}

fu= nction json_decode(string $data, bool $assoc =3D false, int $depth =3D 5= 12, JSON_FLAG|int $options =3D JSON_FLAG::None) {}

<= div>json_decode($data, options: JSON_FLAG::ThrowOnError | JSON_FLAG::Pre= ttyPrint);

Note that the goal here isn=E2=80=99= t so much to save typing space, as it is to provide easier discoverabili= ty through IDEs, better type safety, and better ergonomics in the langua= ge overall.

Attempting to use a backed value th= at is not a power-of-two would result in a compilation error. Bitwise op= erations are performed as though they=E2=80=99re performed on a regular = integer.

What do you think?

I couldn=E2=80=99t find any similar proposals (listed on the RFC p= age) before proposing this on the list, so if you=E2=80=99re working on = this, please speak up; I=E2=80=99d like to join forces.
=E2=80=94 Rob
--4ee583bdff6c4bd88a5d3247da9bdbee--