Set global immutables from main/getopt?

Andrew Klaassen clawsoon at yahoo.com
Tue Jan 28 09:11:41 PST 2014


I thought it'd be a good idea to set global immutables from args 
passed to main().  When I do this:


import io = std.stdio;

immutable string abc;

void foo() {
   io.writeln(abc);
}

void main(string[] args) {
   abc = args[1];
   foo();
}


...I get the compiler error: "Error: can only initialize const 
member abc inside constructor".  When I do this:


import io = std.stdio;

void foo() {
   io.writeln(abc);
}

void main(string[] args) {
   immutable string abc = args[1];
   foo();
}


...I get the compiler error: "Error: undefined identifier abc".

Is there any workaround, or am I attempting a 
stupidity/impossibility?

Thanks.

Andrew



More information about the Digitalmars-d-learn mailing list