On Sunday, 27 November 2016 at 16:42:17 UTC, Suliman wrote:
Am I right understand that `scope(exit)` should be always at top, otherwise it would not work (it's very strange because by the docs it's calling every time when function out of the scopes)?

No, scope(exit) queues the thing for execution, so it doesn't necessarily need to be at the top.

void test() {
   scope(exit) writeln("1");
    writeln("2");
   scope(exit) writeln("3");
}


That would print "2, 3, 1". The first line queues 1. The second line runs immediately and prints 2. The third line queues 3.

When the function returns, all queued things are played back in reverse order. Thus, 3 goes first, then 1.


If you returned right after the second line, you would only see "2, 1", since the queuing of the 3 would never happen.

Reply via email to