JC,
   AS3 has no block scope.

   Whenever you write 'var x' inside a method, that declaration
(effectively) gets moved to the 'top' of the method.

So at a compiler level, this:

> private function _demo():void {
> for (var i:Number = 0; i < 10;i++) {
>  //evil stuff here
> }
>  for (var i:Number = 0; i < 10;i++) {
>  //evil stuff here
> }
> }

becomes this:

private function _demo():void
{
  var i:Number;
  var i:Number;  /// Second definition!
  for (i=0;i<10;i++)
  {
     // Evil stuff
  }
  for (i=0;i<10;i++)
  {
     // Evil stuff
  }
}

And this:

>  private function _demo():void {
>   for (var i:int = 0; i < 10; i++)
>   {
>    trace (this[j]);
>   }
>   var j:Object = null;
>  }

becomes this:

private function _demo():void
{
  var j:Object;
  var i:int;
  for (i=0;i<10;i++)
  {
    trace(this[j]);
  }
  j=null;
}

I find it totally counterintuitive. It's a language spec thing. It was
the same in AS2 - although, interestingly, Nicolas Cannasse's MTASC
compiler added block scope and made AS2 work 'properly' - so it's not
like it's an issue at bytecode level.

Ian


On Tue, Aug 18, 2009 at 9:28 AM, Hans
Wichman<[email protected]> wrote:
> Hey list,
>
> does anyone know the cool rationale behind the fact that the compiler won't
> allow me to execute a completely sane piece of code such as:
>
> private function _demo():void {
> for (var i:Number = 0; i < 10;i++) {
>  //evil stuff here
> }
>  for (var i:Number = 0; i < 10;i++) {
>  //evil stuff here
> }
> }
>
> (getting a redefined blablah variable, to which there is no workaround but
> to rename the loop variables I think)
>
> But WILL allow me to do something stupid like:
>
>  private function _demo():void {
>   for (var i:int = 0; i < 10; i++)
>   {
>    trace (this[j]);
>   }
>   var j:Object = null;
>  }
>
>
> :)
>
> JC
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>

_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to