Can [] be made to work outside contexts of binary operators?

Dmitry Olshansky via Digitalmars-d digitalmars-d at puremagic.com
Thu Oct 22 10:38:02 PDT 2015


On 22-Oct-2015 18:57, 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.


Hm writeln supportы cool printing of any range might be not quite what 
you want though.

e.g.

auto arr = [1.23, 5.46, 6.21, 7.7711, 9.81121];
writeln("%(%.2f, %)", arr); // would print coma separated list



-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list