www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - problem with args

reply Chris Paterson <chris paterson.bz> writes:
I can't see what's going wrong here.  Compiling this program:
===
import std.stdio;
import std.conv;

int main(char[][] args)
{
    if (args.length > 0) {
        const int max = std.conv.toInt(args[1]);
        writefln("max=%d", max);
    }
    return 0;
}
===
with "dmd args_test.d"
gives an error message:
"Error: variable args is used before initialization"
with no line number or any other information.

It's the line:
        const int max = std.conv.toInt(args[1]);
that causes the problem.

What am I missing here?

Thanks,
Chris.
Sep 15 2007
parent Christian Kamm <kamm.incasoftware shift-at-left-and-remove-this.de> writes:
Chris Paterson wrote:

 int main(char[][] args)
 {
  if (args.length > 0) {
   const int max = std.conv.toInt(args[1]);
   writefln("max=%d", max);
  }
  return 0;
 }
const in D 1.0 means compile time constant and neither args[1] nor the call to toInt can be evaluated at compile time. Remove the const and it will compile. Also change the check to args.length > 1 though. It's the same for 2.0 at the moment, I think. Probably you could use final there to get the effect you desired, but final might be removed in the future. Remember to use the digitalmars.D.learn newsgroup for questions like this. Cheers, Christian
Sep 15 2007