Leo's Technical Blog

Gotcha with Flex, Function Signatures and Method Overriding

Introduction

user

Leo Soto


tips, flex

Gotcha with Flex, Function Signatures and Method Overriding

Posted by Leo Soto on .
Featured

tips, flex

Gotcha with Flex, Function Signatures and Method Overriding

Posted by Leo Soto on .

Yesterday, I lost around 2 hours debugging a weird problem. When extending flash.utils.Proxy (which allows you to implement dynamic properties/methods on AS3, similar to __getattr__ in Python or method_missing in Ruby) I did the needed implementation of the callProperty method:

   flash_proxy override function callProperty(methodName:*, ... args):*

But on runtime, it crashed with the following error:

VerifyError: Error #1053: Illegal override of foo.bar.CustomProxy:

        at WebDAVClient/testODS()
        at WebDAVClient/___WebDAVClient_Button7_click()

Which wasn't a very helpful message, by the way. Looking at the console I got this message, which was better:

param 1 incompatible
   virt * flash.utils::Proxy/http://www.adobe.com/2006/actionscript/flash/proxy::callProperty()
   over String foo.bar::CustomProxy/http://www.adobe.com/2006/actionscript/flash/proxy::callProperty()

But it still didn't made sense, because, as you see above, the method signature I wrote does not has any "String" type declaration.

After a lot of trial and error, I found the cause of the problem: In the middle of the implementation I declared a local variable named methodName which, as astute readers are realizing now, collides with name of the first argument of callProperty:

   flash_proxy override function callProperty(methodName:*, ... args):*
   {
       // Irrelevant code for this blog post
       // ....

       var methodName:String = someCalculation();
       //  ^^^^^^^^^^
       //  This declaration causes type mismatch
       //  problem on the function signature!

       // Rest of the implementation, also irrelevant
       // ...
   }

So, looks like the declaration of the local variable methodName shades the declaration of the parameter methodName... when flash determines the signature of the method!

Sure, in part this was my own stupidity for accidentally naming a local variable the same as a function parameter. But at least half of the (dis)credit is for Flash compiler/runtime, for (a) not finding the problem (after all, if I'm paying the static-typing tax with the verbose type annotations, I expect some basic checks in return from the compiler) and (b) this obscure "feature" in which a declaration seems to be overriding another.