Global variables read at compile time?

Jesse Phillips jessekphillips+D at gmail.com
Wed Aug 15 07:15:16 PDT 2012


On Wednesday, 15 August 2012 at 13:36:26 UTC, Stefan wrote:
> Hi there, I'm having trouble getting the following code to 
> compile:
>
> import std.stdio;
>
> string a = "a";
> string b = a;
>
> void main()
> {
>     writeln(b);
> }
>
> DMD spits out the error "test.d(4): Error: variable a cannot be 
> read at compile time". Is there any way to tell the compiler I 
> want b evaluated at runtime, or am I missing something obvious 
> here?

untested

string a = "a";

static this() {
     string b = a;
}

be aware of the details:

http://dlang.org/module.html#staticorder


More information about the Digitalmars-d-learn mailing list