Thread overview
Extending bitfields to enumerations of any enumerator type
18 hours ago
Per Nordlöw
17 hours ago
Timon Gehr
16 hours ago
monkyyy
18 hours ago

Given

struct S {
	Field field:1;
}

, is there a reason why

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

enum Field : ubyte { none, dot }

string toString(in Field arg) pure nothrow @nogc {
	final switch (arg) with (arg) {
	case none: return [];
	case dot: return ".";
	}
}

.

17 hours ago
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.
16 hours ago

On Wednesday, 8 October 2025 at 20:37:02 UTC, Per Nordlöw wrote:

>

Given

struct S {
	Field field:1;
}

, is there a reason why

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

enum Field : ubyte { none, dot }

string toString(in Field arg) pure nothrow @nogc {
	final switch (arg) with (arg) {
	case none: return [];
	case dot: return ".";
	}
}

.

string is a very bad example, it be 128 bits
char could be more reasonable, but are you defining a 5 bit lower case format?
unions already exist and bitshifts to compress rapidly stop making sense for things that airnt bools