Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

How to get simplicity of writefln in Tango

Moderators: larsivi kris

Posted: 02/12/08 17:12:09

Tango code:

import tango.io.Stdout;
Stdout.formatln("Lucky number is {}", 42);

Can someone fill in the BLANK below so that the two snippets do the same thing?:

import tango.io.Stdout;
fmtln(...) {
   **BLANK**
}
fmtln("Lucky number is {}", 42);

Thanks!

Author Message

Posted: 02/12/08 22:14:03

Not sure what your looking for... but I typically use Stdout like the following

import tango.io.Stdout;

Stdout("Lucky Number is ")(42).newline;  

you can also use .flush in place of .newline if you dont want the \n

Posted: 02/12/08 22:57:57 -- Modified: 02/12/08 22:58:56 by
aarti_pl

Well, below should probably work (not tested):

import tango.io.Stdout;
fmtln(T...)(T p) {
   Stdout.formatln(p);
}

Posted: 02/13/08 00:22:59

the template function above would generate a new instance for each variantion of argument types.

I would suggest something like this:

void fmtln( char[] frm, ...) {
  Stdout( Stdout.layout.convert( _arguments, _argptr, frm )).newline;
}

Posted: 02/13/08 00:31:35

aarti_pl wrote:

Well, below should probably work (not tested):

import tango.io.Stdout;
fmtln(T...)(T p) {
   Stdout.formatln(p);
}

Oh yeh, good point Aarti. That should work (if you give it a return type). How about the other way, though, using varargs? (Since that's Stdout.formatln uses in the end anyway). Getting off my lazy duff, here are some working, tested examples.

typeof(Stdout) fmtln(char[] fmt, ...) {
   Stdout.layout()(&Stdout.sink, _arguments, _argptr, fmt);
   return Stdout.newline;
}

And just for good measure here's a working version that goes the template route:

import tango.io.Stdout;
typeof(Stdout) fmtln(T...)(char[] fmt, T p) {
   return Stdout.formatln(fmt, p);
}

I would love for there to be a module with wrapper functions like these so that it is not necessary to repeatedly specify "Stdout" everywhere when writing quick & dirty scripts.