Is flags enum needed in Phobos?

Dmitry Olshansky dmitry.olsh at gmail.com
Wed Sep 26 10:52:51 PDT 2012


On 26-Sep-12 21:39, Denis Shelomovskij wrote:
> 25.09.2012 21:14, Denis Shelomovskij пишет:
>> .NET has FlagsAttribute, Java has EnumSet. Looks like we need this too.
>> How about to add a library solution to Phobos?
>>
>> My variant is here (search for `flagEnum`):
>> https://bitbucket.org/denis_sh/misc/src/tip/stdd/typecons.d
>>
>> It has a bug and I have no idea how to fix it:
>> `AB.init |= AB.a` and `AB.a |= AB.a` are allowed.
>> (no, we can't make `AB.a` const to disallow the second case because it
>> will disallow this: `auto a = AB.a; a |= AB.a;`)
>>
>> Also I'm not sure:
>> * Should we support converting from a number to a flag enum?

I'd suggest an explicit way to do so. Like to!(flagEnum) or a standalone 
helpers.

>> * If so, should we support values not from enum flags or throw
>> exceptions (it may be configurable)?
If to! is used I guess throw exceptions if it doesn't fit.

>> * Is `flagEnum` an appropriate name?

Since it's a type (right?) I'd think EnumFlag, FlagSet or even Java's 
EnumSet. Generally I think I like words 'set' and/or 'flag' in the name.

> OK. Looks like such functionality isn't needed and I don't have to do a
> pull request. What about to close
> http://d.puremagic.com/issues/show_bug.cgi?id=6946
> with WONTFIX?
>
First, calm down. Second, I, for one, like it.
The place to go is most likely std.bitmanip or std.typecons. I haven't 
looked much at the actual code but the usage seems straightforward.

Another thought is to generalize it to something beyond 1-bit flags. How 
about packing various flags (set of 3-state flag, 4-state flag  etc.) in 
one fixed struct?


-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list