Skip to main content
Participant
December 3, 2009
Question

Warning: 3596: Duplicate variable definition and Adobe Bug Tracking system.

  • December 3, 2009
  • 1 reply
  • 4259 views

I've recently switched to CS4 and AS3. There is a couple of nice features in AS3. However, the more I do hacking (certainly you can't call it programming) the more frustrated I'm with AS3.

Here is the most stupid thing I found so far.

Put this code in:

for (var i:Number=0; i<10; i++)

{

     trace("i exists and equals: "+i);

}

for (var i:Number=0; i<10; i++)

{

     trace("new for loop; new i: "+i);

}

This is the basic of programming. You create 'for' loop and a variable. Loop ends. Variable should be destroyed. You create another 'for' loop and declare new i variable. Simple. If you publish the SWF movie and select AS2, it will work. But change it to AS3 and "fun" begins! You get "Warning: 3596: Duplicate variable definition" error. I honestly can't understand why Adobe made this kind of a "feature".

I looked up this error in the Internet and found this comment from 2007!

http://curtismorley.com/2007/06/21/flash-cs3-flex-2-as3-warning-3596-duplicate-variable-definition/

So either you can use different variable name or you should omit variable declaration in the second loop. That's "great". Completely in opposition to all modern programming languages. Nice. What's even more bizarre is the fact that this "feature" exists for at least two years!

I wanted to check if somebody have reported this bug already. I went to Adobe Bug System: https://bugs.adobe.com/asc/

As it is written: "Search (Browse without registration)". So I want to browse... where are all bugs? "You are not logged in, and do not have the permissions required to browse projects as a guest." Great! Another "nice feature". I created an account and have been waiting for confirmation e-mail for around an hour. Once I get it, I'll report it. But still... so many nice things were added to AS3 and so many basic things were broken... and the software is more and more expensive!

PS I know I can use var i:int in AS3. I used Number for backward compatibility. Still doesn't make any change that such a basic thing like a for loop behaves in a such a strange way.

This topic has been closed for replies.

1 reply

December 3, 2009

It's not strange, or in opposition to any strongly typed languages - and it is just a warning - it will still run fine. The way to handle it is to simply declare the var prior to using it in the for loops:

var i:int;

for (i = 0; i < 10; i++)

{

     trace("i exists and equals: "+i);

}

for (i = 0; i < 10; i++)

{

     trace("new for loop; new i: "+i);

}

And why not use int - you are compiling for AS3. Forget about AS2.

iaforekAuthor
Participant
December 4, 2009

It's strange for any programmer and it's silly. You can't call AS2 a strongly typed language and in AS2 there was no warning. As far I see AS3 moves away from weakly typed language (well at least when you compare it to AS2), which was a good decision. Code should run faster and be more cleaner but this tiny thing makes everything upside down.

PS I forgot about AS2 As I wrote, I use Number only to show that the code was running fine (no warning) in AS2.

Inspiring
February 16, 2012

The problem also exists with switches:

switch(somevar) {

     case 0: {

               var x:int = 5;

               x = dosomething(x);

          }

          break;

     case 1: {

               var x:int = 10;

               x = doanotherthing(x);

          }

          break;

     case 2: {

               var name:String = "asdf";

               //do something with name

          }

          break;

}

trace(x); //x still exists at this point in AS3. But it shouldn't exist anymore at this point in a good language like C/C++.

What the flash compiler does wrongly is bring the x variable out of the switch's scope. A good programming language like C/C++ enables a user to keep variables as deeply locally scoped as possible. That way memory space can be freed by pop instructions earlier from the stack as soon as it goes out of scope thus making your program more efficient. I have that problem often where i have to keep using a different variable name. And if you want to avoid that then you would have to declare the variable outside the switch. But this is also kind of stupid coz what if other cases of the switch does not make use of that variable like in case 2, then declaration of x is wasted memory allocation on the stack (Note it would get pushed on the stack because flash initializes the value to zero).