Thread overview
Trailing spaces in lists: bug or feature?
Jun 16, 2013
monarch_dodra
Jun 16, 2013
Timon Gehr
Jun 16, 2013
Peter Alexander
Jun 16, 2013
monarch_dodra
June 16, 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?
June 16, 2013
On 06/16/2013 01:01 PM, monarch_dodra wrote:
> 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:
>
> ...
> 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:
>...
> I'm even less sure that this is a feature... Well... Is it?

Feature. Eg. http://dlang.org/expression.html#ArgumentList :

ArgumentList:
    AssignExpression
    AssignExpression ,               // <- here
    AssignExpression , ArgumentList
June 16, 2013
On Sunday, 16 June 2013 at 11:01:39 UTC, monarch_dodra wrote:
> 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.

It's in the grammar and intentional for the reasons you listed.

EnumMembers:
    EnumMember
    EnumMember ,
    EnumMember , EnumMembers

The second rule is the one that allows it. There's similar rules for the other cases you noticed.
June 16, 2013
On Sunday, 16 June 2013 at 11:15:58 UTC, Timon Gehr wrote:
>
> Feature. Eg. http://dlang.org/expression.html#ArgumentList :
>
> ArgumentList:
>     AssignExpression
>     AssignExpression ,               // <- here
>     AssignExpression , ArgumentList



On Sunday, 16 June 2013 at 11:21:29 UTC, Peter Alexander wrote:
> On Sunday, 16 June 2013 at 11:01:39 UTC, monarch_dodra wrote:
>> 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.
>
> It's in the grammar and intentional for the reasons you listed.
>
> EnumMembers:
>     EnumMember
>     EnumMember ,
>     EnumMember , EnumMembers
>
> The second rule is the one that allows it. There's similar rules for the other cases you noticed.

Ah... I saw those lines, but didn't quite interpret them correctly.

Thanks for the answer.