Thread overview
Trailing comma in variable declaration
Jul 29, 2018
Ky-Anh Huynh
Jul 29, 2018
Jonathan M Davis
Jul 31, 2018
Ky-Anh Huynh
Jul 29, 2018
PaperBoy
Jul 31, 2018
Ky-Anh Huynh
July 29, 2018
Hi,

is it nice to have a trailing comma in variable declaration:

[code]
  bool
    verbose = false,
    download_only = false,
    no_confirm = false,
    show_help = false,
    show_version = false,
    list_ops = false,
    ;
[/code]

As trailing comma is possible (and it's great) for arrays, enum,... I wonder why we don't have this fancy thing for variables declaration.

Thanks for your reading.
July 29, 2018
On Saturday, July 28, 2018 11:32:15 PM MDT Ky-Anh Huynh via Digitalmars-d wrote:
> Hi,
>
> is it nice to have a trailing comma in variable declaration:
>
> [code]
>    bool
>      verbose = false,
>      download_only = false,
>      no_confirm = false,
>      show_help = false,
>      show_version = false,
>      list_ops = false,
>      ;
> [/code]
>
> As trailing comma is possible (and it's great) for arrays, enum,... I wonder why we don't have this fancy thing for variables declaration.
>
> Thanks for your reading.


Well, most folks would just make those separate statements, in which case, there would be no commas at all. Some folks do put multiple variable declarations on a single line, but if you do that, a trailing comma looks terrible. I doubt that using a single statement to declare multiple variables but putting it on multiple lines was even a use case that was really considered. Also, a quick test with a C++ compiler shows that it's not legal there, so the rules we have with regard to this probably just came from C++. AFAIK, the only significant change that D has from C/C++ with regards to declaring multiple variables in a single statement is that the * is considered part of the type and thus

int* a, b, c;

declares three variables of type int* in D, whereas in C/C++, it would declare a single variable of type int* and two of type int.

- Jonathan M Davis



July 29, 2018
On Sunday, 29 July 2018 at 05:32:15 UTC, Ky-Anh Huynh wrote:
> Hi,
>
> is it nice to have a trailing comma in variable declaration:
>
> [code]
>   bool
>     verbose = false,
>     download_only = false,
>     no_confirm = false,
>     show_help = false,
>     show_version = false,
>     list_ops = false,
>     ;
> [/code]
>
> As trailing comma is possible (and it's great) for arrays, enum,... I wonder why we don't have this fancy thing for variables declaration.
>
> Thanks for your reading.

This is the syntax sometimes seen with language that don't have trailling comma at all.

bool
       verbose = false
     , download_only = false
     , no_confirm = false
     , show_help = false
     , show_version = false
     , list_ops = false
     ;


July 31, 2018
On Sunday, 29 July 2018 at 10:46:12 UTC, Jonathan M Davis wrote:
> 
> Well, most folks would just make those separate statements, in which case, there would be no commas at all. Some folks do put multiple variable declarations on a single line, but if you do that, a trailing comma looks terrible. I doubt that using a single statement to declare multiple variables but putting it on multiple lines was even a use case that was really considered. Also, a quick test with a C++ compiler shows that it's not legal there, so the rules we have with regard to this probably just came from C++. AFAIK, the only significant change that D has from C/C++ with regards to declaring multiple variables in a single statement is that the * is considered part of the type and thus
>
> int* a, b, c;
>
> declares three variables of type int* in D, whereas in C/C++, it would declare a single variable of type int* and two of type int.
>
> - Jonathan M Davis

Thanks a lot for your explantion, Jonathan.

I understand the idea is to minimize the differences between C/C++ and D (FIXME.) Anyway the missing feature suprised me; I thought variable declaration is simply a (compile time) list, and as an array, leading comma was acceptable :)

July 31, 2018
On Sunday, 29 July 2018 at 14:47:59 UTC, PaperBoy wrote:
>> As trailing comma is possible (and it's great) for arrays, enum,... I wonder why we don't have this fancy thing for variables declaration.
>>
>> Thanks for your reading.
>
> This is the syntax sometimes seen with language that don't have trailling comma at all.
>
> bool
>        verbose = false
>      , download_only = false
>      , no_confirm = false
>      , show_help = false
>      , show_version = false
>      , list_ops = false
>      ;

This is smart:) It's not very natural I think; in English, comma and punctuation would be closed to the last character (e.g., the dot in this sentence.)