Extra commas in arrays

void main(){    
  int[3] arr = [1, 2, 3,];    
}

Is the extra comma legal or is it not flagged as an error due to a compiler error? I have many mixes that generate arrays with an extra comma at the end. I would like to know if I should take the time to remove them.

even this compiles without errors:

void main(){    
  int[3] arr = [1, 2, 3,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,];    
}
+5
source share
5 answers

I believe that it is legal to allow templates (or even mixins) to work in a general way:

template Foo(T) { }                       //What if Foo is empty like this?

auto arr = [1, 2, Foo!(int), Foo!(long)];
//         [1, 2, , ]

This simplifies working with templates, so you do not need to specifically protect against special outputs.

A more realistic example:

template Iota(size_t start, size_t end)  //All integers in range [start, end)
{
    static if (start < end)
        alias TypeTuple!(start, Iota!(start + 1, end)) Iota;
    else
        alias TypeTuple!() Iota;
}

auto arr1 = [-10, Iota!(0, 3)];    // arr is now [-10, 0, 1, 2]
auto arr2 = [-10, Iota!(a, b)];    // arr is now [-10, a .. b]

Now, what happens if aequal b? Then arr2splits into [-10, ].

+7
source

, :

string[3] arr = [
    "Some long String",
    "And another",
    "etc, etc, etc",
    ];

.

Java .

+4
+3

99% , . 2-, 3- ..? , , , .

+1

dmd. , , dmd1, .

Now, for dmd2, at least the trailing comma should always be valid in a literal array, as well as in parameter lists, argument lists, and template argument lists.

Numerous trailing commas, however, are implementation errors.

+1
source

All Articles