www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Scope exit bug?

reply Amex <Amex gmail.com> writes:
void foo()
{
    void bar() { foo; }
    switch
        case: scope(exit) { do }  break

    bar;

}

fails to execute do


void foo()
{
    void bar() { foo; }
    switch
        case: bar; do return;

    bar;
}

does work... yet there is no difference except the scope exit.

In my code it is if do is never executed...

I used it to avoid having to write bar twice or use a flag but 
it's not working... I see no reason why it should not work. 
scope(exit) is suppose to execute the block at the end of the 
function call, right?
Jun 13 2019
parent Les De Ridder <les lesderid.net> writes:
On Friday, 14 June 2019 at 05:35:05 UTC, Amex wrote:
 I used it to avoid having to write bar twice or use a flag but 
 it's not working... I see no reason why it should not work. 
 scope(exit) is suppose to execute the block at the end of the 
 function call, right?
No, a scope guard executes at the end of the scope. The spec[1] has a few examples (with output). [1] https://dlang.org/spec/statement.html#scope-guard-statement
Jun 14 2019