print function

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 4 07:32:48 PST 2016


On 02/04/16 15:02, ixid via Digitalmars-d-learn wrote:
> On Thursday, 4 February 2016 at 13:46:46 UTC, Dejan Lekic wrote:
>> On Thursday, 4 February 2016 at 00:23:07 UTC, ixid wrote:
>>> It would be nice to have a simple writeln that adds spaces automatically like Python's 'print' in std.stdio, perhaps called print.

> I have written an attempt at it but my point was that a print function would be a good addition to the standard library rather than asking someone to write an implementation for me.
> 
> string makePrintString(T)(T length) {
>     import std.conv : to;
>        
>     string s = "writeln(";
>     foreach( i; 0 .. length) {
>         s ~= "a[" ~ i.to!string ~ "]";
>         if(i != length - 1)
>             s ~= ",\" \",";
>         else s ~= ");";
>     }
>            
>     return s;
> }   
> 
> void print(A...)(A a) {       
>     static if(a.length) {
>         mixin(makePrintString(a.length));
>     } else writeln;
> }

   void print(A...)(A a) {
      foreach (N, ref e; a)
         write(e, N==A.length-1?"\n":" ");
   }


artur


More information about the Digitalmars-d-learn mailing list