开发者

What frameworks/languages support run-time class creation? [closed]

开发者 https://www.devze.com 2023-01-04 16:09 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago. 开发者_开发知识库

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号