Re: enum flag backed enum

From: Date: Wed, 03 Sep 2025 21:23:38 +0000
Subject: Re: enum flag backed enum
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-128618@lists.php.net to get a copy of this message
Hi Rob, Am 3. September 2025 22:44:58 MESZ schrieb Rob Landers <rob@bottled.codes>: >Hello internals, > >I would like to propose a new type of backed 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 flags. > >In some core functions, it might look like this: > >enum JSON_FLAG: flag { > case None = 0; > case ThrowOnError = 0x1 << 1; > // etc. > case PrettyPrint = 0x01 << 4; > // etc. >} > >function json_decode(string $data, bool $assoc = false, int $depth = 512, JSON_FLAG|int $options >= JSON_FLAG::None) {} > >json_decode($data, options: JSON_FLAG::ThrowOnError | JSON_FLAG::PrettyPrint); > >Note that the goal here isn’t so much to save typing space, as it 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’re performed on a regular integer. > >What do you think? 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 together with generics new EnumSet<JsonOption>. > >I couldn’t find any similar proposals (listed on the RFC page) before proposing this on the >list, so if you’re working on this, please speak up; I’d like to join forces. > >— Rob Marc

« previous php.internals (#128618) next »