On Thursday, 22 October 2015 at 15:57:05 UTC, Shriramana Sharma
wrote:
I tried:
import std.stdio;
void main()
{
int [5] vals = [1, 2, 3, 4, 5];
writefln("A = %d, B = %d, C = %d, D = %d, E = %d", vals []);
}
but got thrown an exception that "%d is not a valid specifier
for a range".
The Python equivalent to flatten a list works:
vals = [1, 2, 3, 4, 5]
print("A = {}, B = {}, C = {}, D = {}, E = {}".format(*vals))
Output:
A = 1, B = 2, C = 3, D = 4, E = 5
Question:
Can D's [] be made to work that way? I recently had to write
custom functions since I had an array representing numerical
fields and wanted to print them out with individual labels but
I wasn't able to use a single writefln with sufficient
specifiers for that purpose because of this limitation.
D's `writefln` is a template-variadic function. Each time you use
it, the compiler looks at the arguments you send to it, and
compiles a new instantiation of it based on the number and types
of these arguments. This means that it would have to know at
compile time how many values `vals[]` holds - but that number is
only known at runtime!
Now, in your case, since vals is a static array, it should be
possible to know it's value at compile time. Maybe if there was a
`tupleof` for static arrays?
At any rate, you can always use the range formatters %( and %) to
print the array. See http://dpaste.dzfl.pl/47e3e5a9e5c4