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 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
> 

Hey Larry, Marc, Morgan,

You are all asking basically the same question; I think: "This looks like 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 is 
important not to have "magic". The power-of-two rule is to make it possible to 
work back how $enum->value === 15 (0x1111) even if you are completely new to 
the language. If you just use some magical cardinal order, it is impossible to 
reserve ranges, handle communications with external 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.

— Rob

Reply via email to