third draft: add bitfields to D

Steven Schveighoffer schveiguy at gmail.com
Thu Jul 4 18:34:00 UTC 2024


On Wednesday, 3 July 2024 at 19:02:59 UTC, Dave P. wrote:
> On Sunday, 5 May 2024 at 23:08:29 UTC, Walter Bright wrote:
>> https://github.com/WalterBright/documents/blob/2ec9c5966dccc423a2c4c736a6783d77c255403a/bitfields.md
>>
>> Adds introspection capabilities.
>>
>> https://github.com/dlang/dmd/pull/16444
>
> Wouldn’t a more D-like syntax be to have the number of bits 
> following the type instead of following the variable name? This 
> would be more consistent with how D arrays are different than C 
> arrays.
>
> Example:
>
> ```
> struct Foo {
>     uint:4 x;
>     uint:5 y;
> }
> ```

This is a great idea. And then you can extract this as a type as 
well!

I see potential for some nice things here. For instance VRP:

```d
uint:4 value(){
    // return 200; // error
    return 7; // ok
}

int[16] arr;
arr[value()] == 5; // VRP makes this valid
```

Walter, is this possible to do? This would make a lot of the 
traits parts unnecessary -- you could introspect the bits just 
from the type. e.g.

```d
enum bitsForType(T : V:N, V, size_t N) = N;
```

-Steve


More information about the dip.development mailing list