(pseudo) Abstract Classes in AS3!

by

(Since we’re MXNA’d now, I wanted to bring this post and maybe a couple of others back up to the top by reposting them. This was written back in October)

I was looking at this blog post by Tink about more strict Abstract constructors in Flash using Errors. I worked out a way that is a bit more formalized. It adds support for abstract methods and unlike Tink’s example, doesn’t require you to type out the string of the class name.

Download the source. Sorry, this link is dead, but the source is now part of AS3Lib
Actually, now it’s here: https://github.com/mimshwright/AbstractAS3

Basically, it works by throwing an error whenever you want a class to be subclassed or a method to be overridden. It’s a runtime error and doesn’t get thrown until you actually try to instantiate or run a method which. While this isn’t perfect it’s better than nothing.

It also checks the type in the constructor which allows you to add code to the constructor that will be inherited by the subclass upon instantiation without causing the errors (in other words, you can safely call super() without getting an error). It wraps up everything into an AbstractEnforcer utility class that simplifies the syntax.

The AbstractEnforcer.enforceConstructor() method uses a second utility function that you may find useful: strictIs(instance:Object, comparisonClass:Class) located in com.mimswright.utils. This function works just like the is operator but performs a strict comparison on the fully qualified class name. The result is that you will only get true back if instance‘s class is exactly the same as comparisonClass (with the is operator you would get true if instance was a subclass of comparisonClass). It’s a bit nicer than doing a string comparison of the class names.

I’m still looking for people to bang on it and see if it breaks or find more improvements so please check it out.

Note: This is AS3 only. You’ll need to have some way to compile AS3 to use this (like Flex Builder). You’ll also have to be a big geek and be into anime and Spawn action figures.
The code is set up to break as it is, but I left some commented code in there so you can try different scenarios.

Thanks to Tink and everyone.

  • http://blog.3r1c.net Eric Cancil

    Yes, this is very nice. Have something similar implemented across several projects I’m working on right now. Still wish it was native ; )

  • http://choppingblock.com Matthew

    hey, thanks for doing this… im in a small fight with Abstract Classes in AS3. I even tried to hassle Roger not so long ago.

  • http://www.as3dp.com Chandima

    I’ve been following your discussions on runtime checks for abstract classes (also Tink’s) for some time and developed a runtime abstract method implementation check using the AS3 introspecion routines. Check it out at:

    http://www.as3dp.com/?p=9

    Appreciate some comments on it. I’ve not used it extensively yet, so I don’t know how scalable introspection will turn out.

  • http://losdesigns.com Mims Wright

    Hey Chandima,

    I actually did figure out a way to enforce methods as well as classes with my implementation. The AbstractEnforcer class contains two methods,
    AbstractEnforcer.enforceConstructor() for making a constructor abstract
    and
    AbstractEnforcer.enforceMethod() for making a method abstract.
    You can use both independently of each other – in other words, you can have an abstract method without having an abstract class.
    Both work at runtime. What would be really great is to get one that works at compile time but I haven’t figured that out yet.
    Your solution is interesting too. There’s probably more to learn from the introspection that I haven’t found yet.

    Mims

  • http://gongised.blogspot.com Olivier

    It might be a bit late, but check out Object.constructor in the docs, it returns the Class of an Object.

    Might replace this line, for example :)
    Class(getDefinitionByName(getQualifiedClassName(instance)));

  • Pingback: Using Abstract Classes in the Flash CS3/4 Library « dispatchEvent() Blogâ„¢

  • Pingback: IDisplayObject? – getting around the lack of an interface for the DisplayObject in Flash « dispatchEvent() Blogâ„¢