views:

49

answers:

4

Hi. 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 :)

+2  A: 

Dynamic languages like Python, Ruby, ...

Darin Dimitrov
+2  A: 

Objective-C supports it (objc_allocateClassPair)

cobbal
Thanks :). And thanks for the reference.
Alix
+1  A: 

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.

Steve314
I meant explicit support, but this is interesting. Thanks :)
Alix
+2  A: 

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();
Rodrick Chapman
Interesting, thanks :)
Alix