1 Reply Latest reply on Nov 2, 2009 2:50 PM by Flex harUI

    Actionscript question: casting an object to an interface...

    CMcM00

      I'm new to Actionscript programming, and am curious about something I've seen in some sample code that I've downloaded.

       

      Apparently the programmer is casting his objects to an interface instead of to the implementation of that interface. What exactly does the compiler do with that? Why would that be the preferred method? One might think the compiler would cast to an interface where its functions are not defined, and then not be able to call these functions because they are essentially abstract functions. I suppose the compiler knows even after casting that this object already inherits this interface and so calls its implemented functions?

       

      A C++ analog perhaps is where a pointer is cast to its base class where abstract functions are declared. Under the covers, the compiler then uses the vtable to lookup the correct function from the subclass to use. Is this basically what the compiler is doing? If so, why not cast to the implementation and not the interface?

       

      In the particular example I'm looking at, the programmer does something like this:

       

        var entity:UIComponent = UIComponent(event.currentTarget);

      var thing:MyData = IDataRenderer(entity).data as MyData;

       

       

      I've even seen parts of the code where member variables are declared as interfaces, not implented classes, and then passed around as if it's a regular variable. ???

       

      I read through some Actionscript docs about interfaces, and this didn't really shed any light on it.

       


      Thanks for the help!

      Cory