defining in a module symbols for export

Jesse Phillips jessekphillips+D at gmail.com
Mon Nov 22 13:20:07 PST 2010


spir Wrote:


> === mod.d ==
> import std.stdio;
> 
> struct S {
>     int i;
>     void speak() {writeln("i: ",this.i);}
> }
> === __trials__.d ===
> import mod;
> 
> auto s = S();
> s.speak();
> s.i = 1;
> writeln(s.i);
> 
> void main () {
> }

Others have answered how to initialize variables, but haven't explained the compilation error. A simple example of what you are doing wrong is:

import std.stdio;

writeln("foo");

void main () {
}

How are you to execute writeln()? You probably wouldn't try this under normal conditions, but is what you are trying to do. You can place it in a static this constructor:

import std.stdio;

static this() { writeln("foo"); }

void main () {
}

Note you can move the calls into main and it compiles fine:

void main () {
s.speak();
s.i = 1;
writeln(s.i);
}



More information about the Digitalmars-d-learn mailing list