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

Simpler console output?

Moderators: kris

Posted: 02/17/07 23:23:06

Apparently there are a few different output functions to choose from in Stdout. This confused me a bit, since I was looking for a kind of be-all-end-all of output functions like writefln in phobos. So there's Stdout("..."); which is easy to type but lacks formatting outside of call chaining. Then there is Stdout.format("..."); and formatln which is longer to type but allows C# style formatting, which is cool. There is also Stdout.print and println, which seem to be the same as Stdout(); and Stdout().newline; (they almost don't seem worth mentioning in a reference doc).

Why not just do something like make Stdout allow formatting information, and make that the canonical console output function?

I also suggest mentioning in the docs that writefln style vararg formatting will not work here. Something to the effect that Stdout.formatln("The number is ",7,"."); will not do what you might think it does when coming from a phobos background.

I think I see why call chaining could be good for this now... Stdout.format("Hello World! The number is {0}\n",42)

("But now the number is {0}\n",43)();

... it would kill any ambiguity arising from multiple strings having formatting. But, that code I just mentioned doesn't do what I expect it to do. oh well.

Author Message

Posted: 02/18/07 06:55:32

Yeah, we've been looking into this for a couple of weeks now. It's kinda tough since we don't want the ambiguities that writef() introduced, and at the same time we want to fold Stdout.print() and Stdout.format() together. I had a slick way to do it, but then the Template thing got in the way (the output can be templated for w/d/char[]).

In the meantime, we decided to at least remove the two formatln() and println() variations, to reduce the choices there. Still working on the best way to do all this.

BTW: the call to Stdout("one", 2, 3) emits <one, 2, 3> for a reason ... that's what you typed in the first place :)

Posted: 02/18/07 15:35:49

Use GDC and go with templates. ;)

Posted: 03/09/07 00:07:06

Correct me if I'm wrong, but wasn't one of the goals of the D Language to make the programmer as productive as possible? Aren't Cin/Cout/Cerr a step backwards toward C++? Specifically, it takes quite a bit more typing to accomplish the same thing with these methods than it does with writef/writefln. And writef/writefln were even more powerful than printf used to be. It would seem a shame to me to not include these powerful methods in a new-and-improved standard library. Just my $0.02. -- Glenn

Posted: 03/09/07 00:13:32

Or could it be that I'm confused?

Are writef and writefln still available from D even when using Tango?

-- Glenn

Posted: 03/09/07 04:30:43

Look at the posts above ... see the references to Stdout? That is the Tango equivalent to writef, whereas Cout is a lower level text-only interface :)

Posted: 02/14/08 05:57:18

kris wrote:

Look at the posts above ... see the references to Stdout? That is the Tango equivalent to writef, whereas Cout is a lower level text-only interface :)

It still requires more typing to do console output in tango than Phobos. I brought this up over here:

writef is basically equivalent to Stdout.format, right?

That's a significant difference in the amount of typing require to me.

Furthermore, you can alias writef since it's just a function. Stdout.format cannot be aliased. If you want to give it a shorter name you have to make a delegate poitner or something

  auto putf = &Stdout.format;

at least I think that would work. Just not as satisfying as a simple alias, though. It takes up memory for instance. Not guaranteed to work if you put it in a module constructor, etc.

I think a functional interface to Stdout's functions is desirable. I will be making one of my own if it is not provided by Tango. I just don't want every durn script I write to be filled up wtih Stdout.blah repeated 1000 times.

Posted: 02/17/08 07:16:07

all good points and we do tend to explicitly avoid overt verbosity in Tango. Yet it should be noted that the absolute brevity of a name is not the #1 priority. In this particular case, it would be nice if the compiler were to allow an alias or something similar.