stdio line-streaming revisited

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Thu Mar 29 09:57:09 PDT 2007


kris wrote:
> Sean Kelly wrote:
> [snip]
>> I must be missing something.  Why is the following not acceptable?
>>
>>     import tango.io.Console;
>>
>>     void main()
>>     {
>>         char[] name;
>>         Cout( "Please enter your name: " ).flush;
>>         Cin.nextLine( name );
>>         Cout( "Hello, " )( name )( "!" );
>>     }
> 
> 
> There used to be a tango/example like this variation:
> 
>     import tango.io.Console;
> 
>     void main()
>     {
>         Cout ("Please enter your name: ").flush;
>         Cout ("Hello, ") (Cin.get);
>     }

Ah, also, the last line is translated into:

Cout.opCall("Hello, ").opCall(Cin.get);

D does not specify evaluation order, so the code might end up printing 
"Hello, " before reading the standard input. It's funny this does not 
happen exactly because of buffering, but the program has no control over 
the buffering so it should assume flushing could happen at any time. So 
the correct code is:

auto name = Cin.get;
Cout("Hello, ")(name);


Andrei



More information about the Digitalmars-d mailing list