There’s a common thread of posts that have been running on blogs in the Flash community since the first beta releast of ActionScript 3.0. These have to do with making AS3 do stuff that it just doesn’t do, notably, singletons with private constructors, and abstract classes. The problem is, these things just aren’t part of the AS3 / ECMAScript spec, and just aren’t supported. You can’t really do them in AS3. Sure, you can kind of do them, and this is usually accomplished by throwing runtime exceptions when a programmer does something you don’t want him to do.
To me, these kind of solutions are complete hacks and at best are a pale imitation of what the feature really is in a language that supports it. It also seems like a whole lot of work just to make something fit in some predefined category that exists in other languages. An abstract class or singleton with a private constructor just wouldn’t compile if you used them incorrectly. Throwing a runtime exception is worlds apart from that.
Document your singleton as a singleton. If someone tries to create an instance of it anyway, tell them to RTFM. If they don’t, they’ll probably wind up with a runtime exception or some other error anyway. OK, I suppose it’s pretty considerate to at least give them an error that specifically tells them how they screwed up. So if you want to be that nice about it, cool. I’m not saying you shouldn’t throw an exception like that. I’m just saying that the vast amount of discussion on the subject on how to lock singletons down just doesn’t seem warranted. I’m also not saying private constructors aren’t useful, and wouldn’t be a great addition to AS3, but they aren’t there. So until they are, live with it, and don’t jump through hoops to try to fake them.
As for abstract classes, again, not supported, never have been, and I’m not sure whether or not they ever will be in ActionScript. Trying to create them anyway using runtime exceptions just seems ridiculous to me. When I see people trying to do stuff like that, I feel like they are stuck in some other language that has that feature and can’t seem to figure out how to do without it. If you are going to use ActionScritp, then learn ActionScript and program in ActionScript. When you program in, say, Python, you have a different mindset, different tools and features than you do when you program in, say, C++. And vice versa. I suppose you could write some kind of precompiler that would let you code C++ using indents for code blocks instead of curly braces so it felt more like Python. But it would probably be better to just learn C++ and use braces.
I’m probably coming off a bit blunt here, and don’t mean to insult anyone in who has worked on coming up with solutions like this. I just feel the effort is misguided and could better go into other aspects of application architecture. Often when you start creating complex solutions to things that should be simple, it is a sign you are going down the wrong path and could benefit from rethinking your basic assumptions.