Sunday, March 04, 2007

The Flex compiler doesn't spot name conflicts

Beware! The Flex 2.0.1 compiler appears not to notice the reuse of the same identifier if decalred as both static and non-static in (what in any other language would be) the same namespace. The equivalent throws a hard compile-time error in both .NET 3.0 and in J2SE 5. The following example compiles without error. Is this a bug or is it supposed to be like this in AS3?

2 comments:

Jason Kwong said...

Apparently it's designed that way. From this page: http://livedocs.adobe.com/flex/201/html/04_OO_Programming_161_10.html

"It is permissible, however, to define an instance property using the same name as a static property."

and later:

"If an instance property is defined that uses the same name as a static property in the same class or a superclass, the instance property has higher precedence in the scope chain. The instance property is said to shadow the static property, which means that the value of the instance property is used instead of the value of the static property."

Graeme Harker said...

That behaviour seems to be consistent with Java's history of doing automatic inheritance "shadowing". I thought that was considered to have been a design mistake, one of the reasons that the override keyword is mandatory in .NET and presumably the reason for the introduction of the @Overridable thingy in Java 5. Given that there's a mandatory override keyword in AS3 for inheritance (making it just like C#) you'd think Adobe would have been more keen to eliminate the nightmare of automatic "shadowing". The compiler should at least throw a warning that this shadowing is happening (like the Java 5 compiler does). One more item on the Flex 3.0 wishlist.