Trailing spaces in lists: bug or feature?
monarch_dodra
monarchdodra at gmail.com
Sun Jun 16 04:01:35 PDT 2013
I've come to notice something which I *though* was nice, but I
have not yet used it extensively, because I'm unsure it is a
feature. I wasn't able to find an answer in the grammar.
Basically, it would appear that in D, whenever you have a comma
delimited list of things, you are allowed to append a comma after
the last parameter. This can help when declaring lists, or auto
generating code, so you don't have to "special case" the last
item. The most basic example I can think of are enums:
enum E
{
a,
b,
c, //Trailing space
}
So... Bug or feature? Personally, I like this very much. The
"trailing space" problem was "so problematic" in C++, that there
is a pattern to put the comma at the beginning of the next line,
as the first parameter has a tendency to be more stable than the
last. It means you can then comment any line (including the
last), without breaking your code:
//C++enum E
{
a
, b
, c
}
Or, for classes:
MyClass()
: FatherClass()
, param1(arg1)
//, param2(arg2) //Commented out
{}
This D "feature" is nice as it allows equivalent notation for all
parameters, and helps us OCD users align things neatly. It also
means the coder has to worry a bit less about things like commas,
and get on with his life.
I *think* this is D feature, but I'd like confirmation.
--------------------------------------
I've *also* noticed this works for function calls and/or
constructors (yes, I know, they are just functions). EG:
struct S
{
int a;
int b;
}
void foo(int i, int j);
void main()
{
S s = S(
1,
2, //Trailing space
);
foo(
1,
2, //Trailing space
);
}
I'm even less sure that this is a feature... Well... Is it?
More information about the Digitalmars-d
mailing list