Extern/scope issue

DLearner bmqazwsx123 at gmail.com
Sat Apr 3 10:01:42 UTC 2021


'Testmain' imports module 'testmod'.
Both are shown below.
I expected 1,1,2,2.
I got 1,0,2,1 - which speaks to scope/extern misunderstanding

Any ideas?
Best regards

Testmain:

int xvar;
import testmod;
void main() {
    import std.stdio;

    writeln("Entering: main");
    xvar = 1;
    writeln("xvar=", xvar);
    testsub();
    writeln("xvar=", xvar);

    writeln("Leaving: main");
}

Testmod:

void testsub() {

    extern(D) int xvar;
    import std.stdio;

    writeln("Entering: testsub");
    writeln("xvar=", xvar);
    xvar = 2;
    writeln("xvar=", xvar);
    writeln("Leaving: testsub");
}



More information about the Digitalmars-d-learn mailing list