www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "%s"-format template function arguments

reply vladdeSV <v vladde.net> writes:
Hello people of D-land.

In a template function, I want to format all arguments as if it 
was an array. Se this snippet of code:

     foo(1,2,3);

     void foo(T...)(T args)
     {
         writefln("expected: %s", [1,2,3]);
         writefln("actual: %s", args);
     }

The code above will output

     expected: [1, 2, 3]
     actual: 1

How would I go on about to print all the arguments as I expected 
it, using "%s"?

Best regards,
Vladimirs Nordholm

---

P.S.
I do not understand why only a `1` is printed in the actual 
result.
Apr 15 2018
next sibling parent ag0aep6g <anonymous example.com> writes:
On 04/15/2018 02:04 PM, vladdeSV wrote:
      foo(1,2,3);
 
      void foo(T...)(T args)
      {
          writefln("expected: %s", [1,2,3]);
          writefln("actual: %s", args);
      }
 
 The code above will output
 
      expected: [1, 2, 3]
      actual: 1
 
 How would I go on about to print all the arguments as I expected it, 
 using "%s"?
writefln("%s", [args]); Or avoiding the allocation: import std.range: only; writefln("%s", only(args)); You could also change the `args` parameter to give you a stack-based array: void foo(T)(T[] args ...) { writefln("%s", args); } [...]
 P.S.
 I do not understand why only a `1` is printed in the actual result.
This: writefln("actual: %s", args); becomes this: writefln("actual: %s", args[0], args[1], args[2]); So there are three arguments after the format string. The %s placeholder only refers to the first one. The others are ignored.
Apr 15 2018
prev sibling next sibling parent Alex <sascha.orlov gmail.com> writes:
On Sunday, 15 April 2018 at 12:04:19 UTC, vladdeSV wrote:
 Hello people of D-land.

 In a template function, I want to format all arguments as if it 
 was an array. Se this snippet of code:

     foo(1,2,3);

     void foo(T...)(T args)
     {
         writefln("expected: %s", [1,2,3]);
         writefln("actual: %s", args);
     }

 The code above will output

     expected: [1, 2, 3]
     actual: 1

 How would I go on about to print all the arguments as I 
 expected it, using "%s"?

 Best regards,
 Vladimirs Nordholm

 ---

 P.S.
 I do not understand why only a `1` is printed in the actual 
 result.
If you define foo like that, then, you advice D to handle the input as separate objects. That's ok. But then, you have to define that you still assume, they belong together, like an array. In this example the solution is simple: ´´´ void main() { foo(1,2,3); } void foo(T...)(T args) { import std.stdio : writefln; import std.range : only; writefln("expected: %s", [1,2,3]); writefln("actual: %s", args.only); } ´´´ However, there will be problems, if the types of elements differs: While the template foo will be able to handle this, the std.range : only function won't. It assumes at least something common across them. https://dlang.org/library/std/range/only.html
Apr 15 2018
prev sibling parent Dennis <dkorpel gmail.com> writes:
On Sunday, 15 April 2018 at 12:04:19 UTC, vladdeSV wrote:
 How would I go on about to print all the arguments as I 
 expected it, using "%s"?
You can expand the template arguments into an array by putting it into square brackets: [args]. You can format an array with the default notation using %s, for a custom format you can use %( and %). See https://dlang.org/phobos/std_format.html ``` void main() { foo(1, 2, 3); } void foo(T...)(T args) { writefln("%s", [args]); // prints [1, 2, 3] writefln("%(%s; %)", [args]); //custom ; separator, prints 1; 2; 3 } ```
Apr 15 2018