Extending bitfields to enumerations of any enumerator type

Timon Gehr timon.gehr at gmx.ch
Wed Oct 8 21:39:10 UTC 2025


On 10/8/25 22:37, Per Nordlöw wrote:
> Given
> 
> ```d
> struct S {
>      Field field:1;
> }
> ```
> 
> 
> , is there a reason why
> 
> ```d
> enum Field : string { none = [], dot = "." }
> ```
> 
> isn't allowed and errors as
> 
> ```
> (1,34): Error: bitfield type `FieldNamePrefix` is not an integer type
> ```
> 
> ?
> 
> Instead I currently have to encode this as
> 
> ```d
> enum Field : ubyte { none, dot }
> 
> string toString(in Field arg) pure nothrow @nogc {
>      final switch (arg) with (arg) {
>      case none: return [];
>      case dot: return ".";
>      }
> }
> ```
> 
> .

`enum Field : string { ... }` is internally stored using the layout of a 
string. And you can put other string values in there using casts:

```d
import std;
enum Field : string { none=[], dot="." }

void main(){
     auto f=cast(Field)"..";
     writeln(f);
}
```

So this type actually stores more information than one bit.

Not applicable to strings, but arithmetic operators don't promote to the 
base type:

```d
import std;
enum Bits { none=0, bit0=1, bit1=2, bit2=4 }

void main(){
     Bits b=Bits.bit0|Bits.bit1|Bits.bit2;
     writeln(b);
}
```

This is actually used in the wild for bit flags.

I.e., you can't really be sure that an `enum` type variable holds one of 
the blessed values.

I am not particularly fond of this design.


More information about the Digitalmars-d mailing list