Unused variable warning

I have a variable named i like below code , but once I write down that code block I get a warning that variable i is unused. But It is used actually. I wonder that Should I ignore this warning or should I change the code ?

 override public function execute() : Void
    {
        var timer : Timer = null;
        var i : Int = 0;
        var cmd : ICommand = null;
        super.execute();
        _maxCount = length;
        _count = 0;
        if (commandDelay > 0)
        {
            timer = new Timer(commandDelay, _commandArray.length);
            timer.addEventListener(TimerEvent.TIMER, delayedDoCommand);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
            timer.start();
        }
        else
        {
            for (i in 0..._commandArray.length)
            {
                cmd = _commandArray[i];
                cmd.addEventListener(Command.COMMAND_END, nextCommand);
                cmd.execute();
            }
        }
    }

Error code :
CommandParallel.hx:63: chars 12-13 : Warning: Unused variable

Line 63 is :
var i : Int = 0;

ActionScript:

var i:uint = 0;
for (i = 0; i < 100; i++) {}

Haxe:

for (i in 0...100) {}

The var declaration for i is implied in Haxe within the for scope so your code is a little like this ActionScript:

var i:uint = 0;
for (var i2:uint = 0; i2 < _commandArray.length; i2++) { ... }

hmm, so I shouldnt touch anything because it is 0 at least?

I mean that in your code above the var i : Int = 0; declaration really is unused. You can delete it and your code will work just like you expect here.

Haxe allows multiple declarations of the same variable name (unlike ActionScript) so you can for (i in start...end) {} all day without having to declare i in advance

I got the point right now. Thanks for explanation.

Another thing to keep in mind with haxe.

for (i in 0...10)
{
    i = i + 1; //This won't work.
}

That i looks and behaves like an Int but it is not really an int. You can’t touch it. To make a somewhat custom for loop you will need a while. For example:

var i:Int = -1;
while (++i<10)
{
    i = i+1; //totally legit.
}
1 Like