Monday, February 21, 2011

Actionscript duplicate variable definition

if(true) {
  var a:int = 1;
}
else {
  var a:int = 2;
}

In the above actionscript code I get duplicate variable definition
error because "a" has been declared twice.
But, doesn't "a" exist in two "different" scopes?

Is there an elegant way of removing this warning, rather
than pulling "a" out of both the scopes and moving it outside
the if block?

I need that alternate solution since it is in just too many places
in a code which I have to refactor.

From stackoverflow
  • No, as JavaScript, ActionScript has only one possible scope - function. {} does not create new scope, only function() {} does.

  • There is no elegant solution, other than pulling out the variable or the second time just referencing the variable, not declaring it.

    Check out this answer as well:

    http://stackoverflow.com/questions/2316605/want-to-remove-warnings

  • yup, all you need to do is

    if(true) {
      var a:int = 1;
    }else {
      a= 2;
    }
    

    and your error will be gone

0 comments:

Post a Comment