DIP: add Bit Fields
Walter Bright
newshound2 at digitalmars.com
Mon Apr 22 16:16:07 UTC 2024
Thank you for your thoughts on this. I've thought a lot about it, too!
In the very rare case of actually needing an exact layout, with 5 minutes of
effort any layout can be duplicated with C bit fields. If that's too much
effort, one can write explicit shift/mask code, and encapsulate it into a
function. (Once the function is inlined, there is zero extra overhead for it.)
Or one can use std.bitmanip to do that automatically.
Yes, we can add extra syntax and semantics to make this controllable by the
user, but it's so rarely needed it's over-engineering.
The preceding is true if one wants a specific layout.
But to algorithmically match what the associated C compiler does, that is more
of a significant problem. It took me a while (with the help of Iain) to
reverse-engineer it. This is a problem worth solving, and our implementation
does solve it. For the user, it effortlessly resolves the issue of binary
interoperability with C code.
Good language design means building things into the core language that are the
most common use cases, and provide facilities for the uncommon cases. This is
why slices are builtin, rather than done with metaprogramming.
An earlier version of D had complex numbers builtin. This did have some
advantages over `struct Complex`. But Andrei told me it wasn't worth it. I
eventually agreed with him, and took it out. But, for legacy compatibility, that
had a very long deprecation period.
Let's spend our limited complexity budget on problems that need to be solved,
and explicit bitfield layout isn't one of them.
More information about the dip.development
mailing list