best replacement for - cout << "hello D" << endl; ?

Mike Parker aldacron at gmail.com
Wed Jul 11 04:15:46 PDT 2007


Bruce Adams wrote:
> Jarrett Billingsley Wrote:
> 
>> "Bruce Adams" <tortoise_74 at no.spam.ya.hoo.co.uk> wrote in message 
>> news:f71fdt$rfp$1 at digitalmars.com...
>>
>>>   I'm trying to find the 'best' replacement for C++ streaming I/O.
>>> I dislike C style format strings because of the type safety issue and run 
>>> time interpretation issues. I'm pretty sure D works around this using a 
>>> typeinfo array but I keep losing the function. I understand that writefln 
>>> is now favoured over printf 
>>> (http://www.prowiki.org/wiki4d/wiki.cgi?HowTo/printf).
>> Type safety is not an issue with Phobos' formatting strings; they just 
>> happen to look like printf's formatting.  In fact you can just use %s for 
>> everything, and it'll figure it all out at runtime.  However if runtime type 
>> identification for formatted printing is not what you're looking for, you 
>> won't find any alternative in Phobos.
> 
> std.format.doFormat almost does what I want. It takes any array of TypeInfo objects and another array of arguments so that at there is at least a chance for type safety of some of the conversion being enforced at compile time. 
> 
> What is "dout" for? When would you use it instead of stdout?
>  
>> For that, there's Tango (http://www.dsource.org/projects/Tango), an 
>> alternative community-driven standard library for D which is quickly gaining 
>> popularity.  One of Tango's claims to fame is its much more flexible and 
>> powerful IO framework.  I'm not sure if it still provides the C++ style << 
>> and >> for writing and reading (they may have been removed), but it uses the 
>> same basic idea of chained overloaded operator calls, but using the call 
>> operator instead of the shift operators.  For example, here is a use of the 
>> Stdout object, similar to C++'s cout:
>>
>> import tango.io.Stdout;
>>
>> void main()
>> {
>>     int x = 6;
>>     Stdout("X is: ")(x).newline;
>> }
>>
>> Like in C++ streams, each item is outputted separately, and uses its own 
>> method overload, avoiding runtime type identification.  Objects which read 
>> use the same syntax (called "whisper" for reasons I don't entirely 
>> understand ;) ).
>>
> That is so ugly. I haven't found a page on operator overloading yet but there must be better choices availabe. Even ++ as a binary operator would be better.
>  

Yuck. Operator overloading should not be ambiguous. Each operator should 
mean one thing and one thing only, or as close as possible. When I see 
++ in code, it should indicate that some form of incrementation is going 
on, not that something is being sent to a stream somewhere. I shouldn't 
need to know the context in which the operator is being used in order to 
determine what is going on. There's absolutely no reason to use operator 
overloads for IO when function calls do the job just fine.

The use of << and >> for IO in C++ is one of that languages nastiest 
features. Anything remotely resembling it has no place in D.


More information about the Digitalmars-d-learn mailing list