Open source dmd on Reddit!

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Mar 6 20:17:15 PST 2009


Michel Fortin wrote:
> On 2009-03-06 14:35:59 -0500, Walter Bright <newshound1 at digitalmars.com> 
> said:
> 
>> Andrei Alexandrescu wrote:
>>> "Can't live without bitfields! Give me bitfields and I'll lift the 
>>> Earth!"
>>>
>>> "Here they are, std.bitmanip. Well-defined and more portable and 
>>> flexible than C's."
>>>
>>> "Meh, don't like the definition syntax."
>>
>> Classic.
> 
> Well, he certainly has a point. Compare this:
> 
>     mixin(bitfields!(
>         uint, "x",    2,
>         int,  "y",    3,
>         uint, "z",    2,
>         bool, "flag", 1));
> 
> With this:
> 
>     uint x : 2;
>     int  y : 3;
>     uint z : 2;
>     bool flag : 1;
> 
> The second is certainly prettier and more readable.

(Just to clarify: to me the humor of the situation was that someone who 
considered bitfields an absolute prerequisite for language adoption 
subsequently found the syntax excuse to bail out. Essentially the 
hypothetical user was fabricating one pretext after another to 
rationalize their pre-made decision to not try D -- an absolute classic 
attitude when it comes about acquiring new programming languages.)

About the syntax itself - definitions are few and uses are many. In 
addition the D solution:

(a) guarantees data layout;

(b) offers symbolic limits, e.g. x_max and x_min are automatically added 
as enums;

(c) checks for overflow, which is essential for small bitfields;

(d) offers a way to manipulate the fields wholesale by using the 
concatenation of all their names, e.g. xyzflag;

(e) suggests that there are other cool things that can be done within 
the language, not by adding features to it.

Hopefully that makes up for the more loaded syntax.

> Does it matter much? Not to me; I rarely use bit fields. If I were using 
> them a lot, perhaps I'd be more concerned.

I am using them here and there - even in Phobos - and they work very well.

> While I don't care very much about bitfields, that "mixin(tmpl!(...))" 
> syntax is awful. "mixin tmpl!(...)" is better, but has too many 
> limitations, and it isn't always clear for the user which one should be 
> used. Couldn't D2 get a better syntax for mixins?

I agree it should.


Andrei


More information about the Digitalmars-d-announce mailing list