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 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>`. >> > > 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 the 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 > implementation 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 class > could be added to the standard library; or even have, for each Enum > declared, an EnumSet (final) class 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 constants. I'm not sure if this is necessary, but if so, I'd expect a bit more automation than the initial proposal here. The statement that bitwise operations would fall back to integer ops makes me think it falls 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 collections research with Derick a while back included a `Set` type, which also supported operator overrides for the |, <, <=, -, 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.) So, Rob, which one are you really talking about? --Larry Garfield