stdio line-streaming revisited
Andrei Alexandrescu (See Website For Email)
SeeWebsiteForEmail at erdani.org
Thu Mar 29 12:04:10 PDT 2007
Sean Kelly wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
>> 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.
>
> We discussed this a long time ago and came to the conclusion that while
> the D spec does not guarantee evaluation order for this scenario, it
> seems impossible for it to be anything other than left to right because
> the chained calls rely on the return value from previous calls. If this
> is untrue then I'd love to know the reasoning. Perhaps an aggressive
> inlining mechanism could violate this?
Frits has provided the exact explanation. What you say would be true in
the case:
Cout("Hello, ", Cin.get);
Then it's clear that Cout can't possibly be called before Cin.get
returned. So variadics are the best solution in this case too :o).
Andrei
More information about the Digitalmars-d
mailing list