linuxintellij-ideadgdc

order of method invocation mixed up


I'm trying to learn d so I started with hello world, and tried to expand a little on it.

import std.stdio;
import core.thread;

void main(string[] args){
    writeln("Hello World!");
    Thread.sleep(dur!("seconds")(5));
    writeln("Press enter key to exit...");
    writeln(readln());
}

so I expect my output to be the following

Hello World!
Press enter key to exit...
//input "abcd"
abcd

but instead I get this

//input "abcd"
Hello World!
Press enter key to exit....
abcd

the sleep function even gets skipped. What is happening?


Solution

  • This is a FAQ, when I read the title, I expected to see an IDE and you tagged it, so yay! I can't find my old answer to link to, but the short of it is the output and sleep DO happen, they are just buffered by the IDE pipe and not seen until the end.

    If you add a stdout.flush(); right before the readln and/or right after the first writeln you'll see output - that forces the buffer to go to screen before doing anything else.

    Normal console output will automatically flush on a line, but IDEs are seen as a pipe; the program talking to another program instead of to a user, so it thinks it can buffer by data block instead of by user-visible line.