Charles Hixson escribió:
Andrei Alexandrescu wrote:
Michel Fortin wrote:
On 2009-03-06 14:35:59 -0500, Walter Bright
<newshou...@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
")
Why do the parsing yourself if you can leave that to the compiler? Just
read the docs, pass the arguments and that's it. Way much easier. You
get standard error messages, if you use an IDE you get autocompletion
for the parameters, etc.
I really don't know why everyone is so obsessed with strings... :-P