ActionScript Variables



  • So this one took me about half a day to figure out. I was working on some inherited code that looked something like this:

    public function someFunction(){
      var foo:Class1 = new Class1();
      // Do stuff with foo...
    
      var foo:Class2 = new Class2();
      // Do stuff with foo
    }
    




    I had to add a property to Class2, but if I tried using that property, I kept getting a compiler error. I used the new property in other files and it worked fine, so I had no idea what was wrong. At some point, I tried changing the second foo to foo2 and it started working. Apparently you can redeclare variables within the same method, but the compiler always treats it as the first type it's declared as. The problem never came up because Class1 and Class2 were both children of the same class, and the only properties used were ones that both classes had in common. Allowing variable redeclaration without changing the type makes me say "WTF?"



  • [quote user="Dragnslcr"]Allowing variable redeclaration without changing the type makes me say "WTF?"[/quote]

    I would say it's simpler than that: allowing variable redeclaration makes me say "WTF?"



  • [quote user="JIrvine"]

    [quote user="Dragnslcr"]Allowing variable redeclaration without changing the type makes me say "WTF?"[/quote]

    I would say it's simpler than that: allowing variable redeclaration makes me say "WTF?"

    [/quote]

    Eh, that's ECMAscript for ya. (ECMAscript being the base language behind Javascript and Actionscript. 



  • hehe.. try this one:

    This is fine: 

    class ClassA
    {

      var  variable1:Number = 5;

    }

     This is not

    class ClassA
    {

      var  variable1:ClassB = new ClassB();

    }

     



     



  • You can't redeclare a variable in the same class/function or the compiler will throw an error.  You can make a local variable inside a function with the same name and that will then take precedence over the original although that's only within that function.  As to the second code post; the error received is this:

    A class's instance variables may only be initialized to compile-time constant expressions

    Which is pretty self explanitory.



  • I probably should have mentioned that this was on Flash MX 2004 (yeah, I know, we're planning the migration pretty soon), so more recent versions might have fixed this issue. Being able to redeclare a local variable and have it overwrite the original declaration would be a bit strange, but not a really big WTF. It's the fact that the compiler still thinks the redeclared variable is of the original type. The three possible ways of handling redeclaration I can think of would be

    1. Not allow it at all, which would make the most sense to me
    2. The redeclaration completely overrides the previous declaration
    3. Ignore the redeclaration and treat the statement as a normal assignment, even though the original type of the variable and the type of the value being assigned to it aren't actually compatible.
    Apparently someone thought that going with the third option was a good idea. 


  • hehe I never used MX 2004 as it just sounded waaaay to buggy, went from MX straight over to v8 which seemed like a long (but worth it) wait.


Log in to reply