digitalmars.D - Can [] be made to work outside contexts of binary operators?
- Shriramana Sharma (21/21) Oct 22 2015 I tried:
- Idan Arye (13/33) Oct 22 2015 D's `writefln` is a template-variadic function. Each time you use
- Meta (3/6) Oct 22 2015 Ask and ye shall receive.
- Dmitry Olshansky (8/27) Oct 22 2015 Hm writeln supportы cool printing of any range might be not quite what
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. --
Oct 22 2015
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
Oct 22 2015
On Thursday, 22 October 2015 at 17:19:07 UTC, Idan Arye wrote: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?Ask and ye shall receive. http://forum.dlang.org/post/gkdqakdogqevwzntpgtu forum.dlang.org
Oct 22 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
Oct 22 2015