哪些框架/语言支持运行时类创建?
我正在尝试整理一系列支持运行时类创建的框架/语言。例如,在 .NET 中,您可以使用 System.Reflection.Emit 库在运行时发出新类。如果您可以提及支持此功能(或其某些变体)的其他框架/语言,那将非常有帮助。
谢谢 :)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
动态语言,如 Python、Ruby...
Dynamic languages like Python, Ruby, ...
Objective-C 支持它(objc_allocateClassPair)
Objective-C supports it (objc_allocateClassPair)
在 JavaScript 中,函数是对象。因此,给定一个函数定义:
您可以像这样创建一个新对象:
但在 JavaScript 中,您可以在运行时创建函数。
In JavaScript functions are objects. Thus given a function definition like:
you can create a new object like this:
But in JavaScript you can create functions at runtime.
根据您的意思,任何语言都可以。
例如C++就可以。乍一看,这是荒谬的——C++ 是一种静态类型编译语言。因此,您要做的就是将 LLVM 库包含在您的项目中。这是一个编译器后端,您可以使用它来描述您的类、编译它们并使用 LLVM JIT 运行它们,所有这些都在您的应用程序运行时进行。
IIRC,gcc 后端是用 C 编写的,因此如果您愿意弄清楚该代码,原则上您可以使用甚至没有类的语言在运行时定义类。
无论哪种方式,您工作的一部分是定义类到底是什么 - 它不是内置于编译器后端的,因为它们应该支持具有不同类型系统的一系列不同前端语言。
当然,我并不是推荐这种方法——只是指出它是可能的。
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.