Saturday, April 10, 2010

Is it legal to extend the Class class?

Programmer Question

I've been writing a system that at runtime generates some templates, and then generates some objects based on those templates. I had the idea that the templates could be extensions of the Class class, but that resulted in some magnificent errors:



VerifyError: Error #1107: The ABC data is corrupt, attempt to read out of bounds.


What I'm wondering is if subclassing Class is even possible, if there is perhaps some case where doing this would be appropriate, and not just a gross misuse of OOP. I believe it should be possible, as ActionScript allows you to create variables of the Class type. This use is described in the LiveDocs entry for Class, yet I've seen no mentions of subclassing Class.



Here's a pseudocode example:



class Foo extends Class

var A:Foo = new Foo(); // A is a class
trace(A is Class); // true, right?
var b = new A(); // b is an instance of class A


I have no idea what the type of b would be. Would it be this?



trace(b is A); // true?


But then, could you type the variable b to type A, as in var b:A = new A();, even though A doesn't exist until runtime? I have read that this is not possible as A is not statically resolvable, but I am doing my own tests to explore this possibility.



Ultimately, the purpose is to generate new classes at runtime, and to then create instances of those classes.



In summary: can you subclass the Class class? If so, how can you do it without errors, and what type are the instances of the instances of the Class subclass?



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails