سؤال

If you define a class in actionscript that has the same name as a class in the top level package (e.g. Array) there seems to be no way of explicitly referencing the class in the top level package.

UPDATE: This issue is only appearing in Flash Builder 4.7 with the new ASC 2.0 compiler, using Flash Builder 4.6 with an 'old' Flex SDK it works.

Example:

package
{
    import flash.display.Sprite;

    import mypackage.Array;

    public class AS3Problem extends Sprite
    {
        public function AS3Problem()
        {

            var myOwnArray:mypackage.Array = new mypackage.Array(); 
            // The line below will cause a compile error 
            // 'Ambiguous reference to Array'
            var flashArray:Array = new Array(); 
        }
    }
}

I know the simple solution to this problem is to not create classes with a name that is the same as an as3 top level package class/function, but I'm intrigued as to how this could be 'fixed' in some way by explicitly referring to this package or some other means.

For those interested, I happened to accidentally import 'Array' from hamcrest-as3 while writing tests which caused a problem like this.

هل كانت مفيدة؟

المحلول

Try removing this line

 import mypackage.Array;

so you do not import this custom Array and you always use the fully qualified name, I did not tested it but this should fix it.

EDIT: I tested this and yes you have to add this import line, it will not work , but I can't reproduce the error, I can use and custom Array and the global Array fine with no issue/error or warrning.

EDIT2: This also worked for me, can you test this too?

var globalArray:Class=(getDefinitionByName('Array') as Class);
var arr2:*=new globalArray();
arr2.push("test");
trace(arr2);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top