I'm trying to put together a list of frameworks/languages support run-time class creation. For example in .NET you can use the System.Reflection.Emit
library to emit new classes at run time. If you could mention other frameworks/languages that support this (or some variation of it), that'd be really helpful.
Thanks :)
Dynamic languages like Python, Ruby, ...
Objective-C supports it (objc_allocateClassPair)
In JavaScript functions are objects. Thus given a function definition like:
function Foo(x, y, z)
{
this.X = x;
this.Y = y;
this.Z = z; var g = function()
}
you can create a new object like this:
var obj = new Foo(a,b,c);
But in JavaScript you can create functions at runtime.
function MakeFoo(x, y, z, f) //where parameter f is a function
{
var g = function()
{
this.X = x;
this.Y = y;
this.Z = z;
this.DoSomething = f;
}
return g;
}
var my_class = MakeFoo(a, b, c, function() { /* Do Stuff */ });
var obj = new my_class();
obj.DoSomething();
Depending on what you mean, any language can.
For example, C++ can. At first sight, this is absurd - C++ is a statically typed compiled language. So - what you do is include the LLVM library in your project. This is a compiler back-end, and you can use this to describe your classes, compile them, and run them using the LLVM JIT, all at run-time for your application.
IIRC, the gcc back end is written in C, so if you're willing to figure out that code, you could in principle define classes at run-time using a language that doesn't even have classes.
Either way, part of your job is to define what exactly a class is - that isn't built into the compiler back ends as they are supposed to support a range of different front-end languages with different type systems.
Of course I'm not recommending this approach - just pointing out that it's possible.
精彩评论