Open source dmd on Reddit!
Charles Hixson
charleshixsn at earthlink.net
Sat Mar 7 12:17:34 PST 2009
Andrei Alexandrescu wrote:
> 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
I'm glad that they're there. And I'm glad that they work. But I really
hate the syntax, and am glad I've never needed to use them. MUCH better
would have been:
mixin(bitfields!("
uint, x, 2,
int, y, 3,
uint, z, 2,
bool, flag, 1
")
even better would have been:
mixin(bitfields!("
uint x : 2,
int y : 3,
uint z : 2,
bool flag : 1
")
The bitfield battle isn't one I'm involved with, and I rarely comment on
syntax...but this is, to my mind, a much nicer syntax. Moving the quote
marks to a pre-existing boundary location and removing them internally
is a large gain. There is a small further gain in specifying the
bit-field as a type-name : length string. The colon is a slightly
better delimiter to use here than the comma, as the comma is being used
as the separator between individual bit field specifications. A further
gain is that it's more similar to a form already known by many people.
I'm not a compiler writer, so I don't know if this would have been
difficult. I just know that looking at it *I* find it much more
readable. Another step might have been to allow the entire string to
end with a comma, thus:
mixin(bitfields!("
uint x : 2,
int y : 3,
uint z : 2,
bool flag : 1,
")
so that it would be easier to shuffle the fields during development.
This has it's pluses and minuses, but by and large I think it's optional
presence would be a benefit.
More information about the Digitalmars-d-announce
mailing list