Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128645 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 B0FBC1A00BC for ; Fri, 5 Sep 2025 20:01:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1757102383; bh=Ne/8lxl1IkwExqW5j1yyepm3rynOdDQoQKdgygsn814=; h=Date:From:To:Subject:In-Reply-To:References:From; b=ClBXyE1R+bRWXY9Ea2wGmFQP5TQGt19MsXRDaSKrTa8DS+Ov+hRvZi6h143gm9I77 ly/cIUYTcgoN96/2PGSZ59Fz8ikJL5QAtmptYL8CatdH+ilR7hKP6ony9mtKo1qk7l JWCldxgB8dffEWIheRE9Pozw6hwNNWeZ9mD4oIHJ7T1srCTRTWV7Jtab6sGrsQElX7 iouTNx1NDVew16JNPFVFXGpzOBVKo0OUILkCSjQVb+Qpxp9KRO18XQCUMkO9iy3eLV m8OGYcZEnpTuyPYQW2cKwFg2k+JGiqTkeUZiUAVkNiQjVQJ7hy+LAJH0lPTkDimvCk jF+Sm4m+NGZRA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C33BD18003F for ; Fri, 5 Sep 2025 19:59:39 +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,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-a2-smtp.messagingengine.com (fout-a2-smtp.messagingengine.com [103.168.172.145]) (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 ; Fri, 5 Sep 2025 19:59:39 +0000 (UTC) Received: from phl-compute-12.internal (phl-compute-12.internal [10.202.2.52]) by mailfout.phl.internal (Postfix) with ESMTP id 57D4BEC03D7 for ; Fri, 5 Sep 2025 16:01:07 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-12.internal (MEProxy); Fri, 05 Sep 2025 16:01:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding: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=fm1; t=1757102467; x=1757188867; bh=M+gLdDpslT2lCF2jNOEpUua6SHAdplN0X9TrnxIedsQ=; b= m2rtRAJIBPRlu/xgPqF0QlJWPzgB+qGy0ga6z407M2jWSrMY+57rMNESEke+GBl2 28J1AWvC9SsgTtSTTKlhrl3+TP6/UJzvrAcNxkpZHF3evLzNousaomNxi3OZ/A8r rNR1eFXeCW095ArMYrSoVNAvmR37sMNlrt41NZmZmM+DPTxk3fcxz/9Vb1NUyxDA +ZMI+28K1wkNM5F9Peb34lkCQlAix/Nq1gNvj0Jc0ozqluLSvW9vXB9TQICr00gF vJDvTgLjzCLa1767/ea+AyF42WFIfTUuaEYgumsx6gG4jrELwgbZ3Zw8nzLM6ywa PFMFNEsGRVbucqTLlbbwzQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding: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=1757102467; x=1757188867; bh=M +gLdDpslT2lCF2jNOEpUua6SHAdplN0X9TrnxIedsQ=; b=YouH/DvmG4jb2K+a5 PLUMFyZsF/YTf+UBPH5aAWsQg2Kpui4fyVn3JReTfZDHj4s23MzoifVf/eLHKGf4 ZemauOhJfHbjveojUcsTP3AlaEWRWP+WkL75GD8ZAEzlEhcE/B6ax814BUlcQjuL 5AR2fcUgTKuInv0KbEZzjFbP/RFQWBUN2Fp9E1rNmjxcDtb6ZjMh3uV1mCjQTL9A YL1Mr6TXLHGtiC7s4zzOScytrkPESv5tFO1ojTQrZ/3Tnzh0XqOoTlqO4C9No0s6 5cjERXN4IFUUQul+yNoQntGFyvqmAodN2G+TB6ViD+RRolqycTtoiDq9x1VcOF5X qdWFg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdelkedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceurghi lhhouhhtmecufedttdenucenucfjughrpeffhffvufgfjghfkfggtgfgsehtqhhmtddtre ejnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhnshculgfkoffuohfrngdfuceoihhm shhophdrphhhphesrhifvggtrdgtohdruhhkqeenucggtffrrghtthgvrhhnpeehleffte eigfevudetfedugedtudevledugeeugeelheeihfehgfdtkeevvefgleenucevlhhushht vghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhprdhphhhpse hrfigvtgdrtghordhukhdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhht pdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Fri, 5 Sep 2025 16:01:06 -0400 (EDT) Date: Fri, 05 Sep 2025 21:01:04 +0100 To: internals@lists.php.net Subject: Re: [PHP-DEV] enum flag backed enum User-Agent: K-9 Mail for Android In-Reply-To: References: <9c875e83-ddc8-4c5c-a368-747bef46e4a2@app.fastmail.com> <25e79d21-7b7f-4d7f-b5cb-1e9cfd0aa657@varteg.nz> Message-ID: <7B9311D1-E433-4DBF-A85D-A7DEA94FCC4C@rwec.co.uk> Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On 4 September 2025 15:50:08 BST, Rob Landers wrote: >I think this is a fair observation and a fair question; but I think it is= important not to have "magic"=2E The power-of-two rule is to make it possi= ble to work back how $enum->value =3D=3D=3D 15 (0x1111) even if you are com= pletely new to the language=2E If you just use some magical cardinal order,= it is impossible to reserve ranges, handle communications with external sy= stems, etc=2E A set does not need elements to have a defined order, only a defined ident= ity; that is available on any enum, even one with no backing at all=2E That= pure set could be serialised in various ways, based on the available seria= lisations of its elements (comma-separated list, integer bitmask, binary st= ring bitmask, etc)=2E That's the strongly typed model=2E The weakly typed model is to keep the values permanently in their serialis= ed form, and manipulate that directly=2E That is, you have a set of integer= s for the flags, and construct a new integer for the set of flags=2E That h= as the advantage of being simple and efficient, at the cost of safety and e= asy tool affordance=2E What you're suggesting sounds like somewhere between the two: the individu= al flags are of a specific type, rather than raw integers, but the set itse= lf is just an integer composed of their "backing values"=2E The big downsid= e I see is that you can't natively label a parameter or return value as bei= ng a "set of flags from this enum"=2E You could probably make a docblock ty= pe annotation work with an external static analyser, but in that case, you = might as well use that tool to enforce the powers of 2 on your enum or Plai= n Old Constants=2E Indeed, enforcing "this integer must be a power of 2" is not really anythi= ng to do with enums, it would be useful on *any* type declaration=2E Personally, I find the concept of enum cases having a single backing value= unnecessarily limiting, and would much prefer Java-style case properties= =2E enum FooFlag: object { case None(0, ''); case ThrowOnError(0b1, 'T'); // etc=2E case PrettyPrint(0b1000, 'P'); // etc=2E public function __construct(public int $flagForBinaryApi, public strin= g $flagForTextApi){} } FooFlag::ThrowOnError->flagForBinaryApi; // 0b1 FooFlag:: PrettyPrint->flagForTextApi; // 'P' Ideally we would declare that $flagForBinaryApi must be a power of 2, and = that $flagForTextApi must be a single character, using some general-purpose= feature of the type system=2E=20 The only thing that might be enum-specific is a way to say whether the val= ues of a property must be unique (something we don't currently have with si= ngle-value backed enums)=2E Then the ideal would be an EnumSet customised to work with any properties = or methods it wanted: $foo =3D FooFlag::ThrowOnError | FooFlag::PrettyPrint; $foo->serialiseForBinaryApi(); // 0b1001 $foo->serialiseForTextApi(); // 'TP' Rowan Tommins [IMSoP]