Copy link to clipboard
Copied
This works and traces '2' five times:
for (var i = 0; i < 5; i++)
{
var d:int = 2;
trace(d);
}
I sort of understand why this doesn't:
var d:int = 2;
trace(d);
var d:int = 2;
trace(d);
var d:int = 2;
trace(d);
var d:int = 2;
trace(d);
var d:int = 2;
trace(d);
Since I get the next error:
1151: A conflict exists with definition d in namespace internal
cause I declare the same variable five times.
The thing I don't understand is why it does work in the for-loop, since in principle I'm doing the same thing there. Why isn't it a problem for actionscript when I let the same variable be declared over and over again when I use a for-loop?
Copy link to clipboard
Copied
One way to look at it is that you declare variables for compiler - not runtime. Thus - from the compiler perspective even if you declare var in a loop - it is a single declaration statement.
Copy link to clipboard
Copied
Don't fully understand.
for (var i = 0; i < 5; i++)
{
trace(d);
var d:int = 2;
}
First time the for-loop runs 'd' outputs/equals zero. Then it is declared as being 2. The second time in the loop it outputs 'd' as being 2. Then it is declared again as a variable. But Flash already knows of 'd' now and that it's 2. So shouldn't Flash give the namespace error? Just as if I were to run the 'var d:int = 2' line twice?
Copy link to clipboard
Copied
Compiler catalogues (stores in its internal "database") variables in scopes before runtime. Thus, the d declared in the loop is actually catalogued and declared BEFORE loop execution in the scope of the class that handles timeline which is internal by default. Compiler tolerates duplicate variable declarations in other than class scopes and just gives warning. In other than class scopes compiler declares variable just once for you and overrides duplicates.
In other words - compiler knows already about var d before first trace shows up. Since the variable is int - its default value is 0 - what you see in the trace. If d is Number - first trace will be NaN, if any other datatype - it will be null.
The fact that class scopes do not allow for duplicate variable declarations probably stems from the same root as the fact that there is no overloading in AS3 unlike in other languages.
Another illustration:
var d:Number = 100;
testMe();
function testMe():void
{
for (var i:int = 0; i < 2; i++)
{
trace(d);
var d:String = "s";
}
}
In this case first trace is null although one might expect 100. This is because in the function d is property of function testMe() - not class scope (or namespace).
Copy link to clipboard
Copied
I guess, there is another way to look at it.
All variables that are declared in the program are know to runtime environment. However, there is a decoupling between declaration and assignment.
In your example, Flash is well aware of the fact that var d exists but by the time of the first trace in the loop - value assignment (instantiation) doesn't happen but it happens at the end first loop iteration.
Copy link to clipboard
Copied
Are you saying that before the loop executes AS first checks for variable declarations by looking for var statements and declare any it finds before truely running the loop?
So it looks at my loop and sees 'var d:int=2', so it sets the existence of a d variable first and only one time.
Then it doesn't find any more var statements so it starts running the loop
This time the var statements are ignored and just the assignment(s) of 'd' are set. In this case '2' but could just as well be random numbers.
That's the way it works?
Is that's what is meant by 'variable hoisting' which I read about in the help files?
Where all variable declarations are sent to the top?