在 haxe 中动态创建模拟另一个类(如 remotingProxy)的类

发布于 2024-10-10 18:18:57 字数 304 浏览 0 评论 0原文

我希望能够动态创建一个具有另一个类的所有方法的新类。特别是,我想制作自己的远程代理。 类 ApiProxy 扩展 haxe.remoting.Proxy { } 新类 ApiProxy 将具有与 Api 相同的方法名称和签名,并且将进行编译时检查。只有每个调用都是通过线路完成的,而不是在本地处理。

我认为 remoting.Proxy 是一个神奇的内部类 - 这是真的吗?普通用户是否有能力定义一个像这样强大的静态类?我从未见过用 Java 完成此操作,但 Haxe 中的功能给我留下了深刻的印象。

我将如何实现我自己的类,例如 remoting.Proxy?

I want to be able to dynamically create a new class that has all of the methods of another class. In particular, I want to make my own kind of remoting proxy.
class ApiProxy extends haxe.remoting.Proxy { }
The new class ApiProxy would have all of the same method names and signatures as Api, and would be compile time checked. Only every invocation is done over the wire, instead of handled locally.

I think that remoting.Proxy is a magic internal class - is this true? Do regular users have the ability to define a class that is as powerfully static as this? I've never seen this done in Java and I'm impressed with it's capabilities in Haxe.

How would I implement my own class like remoting.Proxy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜点 2024-10-17 18:18:57

您可以使用宏来实现此目的,

请查看这篇文章以了解想法:

http://haxe.1354130.n2.nabble.com/Macros-Are-Awesome-tc5945711.html

您可以根据类型信息在宏内部写入、保存到文件系统并注册新类已传递给该函数。

you can achieve this with macros

take a look at this post to get the idea:

http://haxe.1354130.n2.nabble.com/Macros-Are-Awesome-tc5945711.html

You can write, save to filesystem and register a new Class inside of a Macro, based on type information that was passed to the function.

反差帅 2024-10-17 18:18:57

haxe.remoting.Proxy 确实很“神奇”,在源代码注释中是这样描述的:
http://code .google.com/p/haxe/source/browse/trunk/std/haxe/remoting/Proxy.hx?r=3592

您无法使用“正常”haxe 代码进行复制。为您自己的类添加/修改 haxe.remoting.Proxy 功能将很困难,但也许并非不可能。
检查“等效”api 实现代码的教程:
http://haxe.org/doc/remoting/proxy

但是,这样的事情可能是可能的使用宏:
http://haxe.org/manual/macros

现在使用宏具有挑战性。文档或示例很少,并且宏功能仍在开发中。但是,它可以让您在编译过程中对编译器进行一些控制,这有时会非常有用。

祝你好运!

haxe.remoting.Proxy is indeed "magic", it is described that way in the source comments:
http://code.google.com/p/haxe/source/browse/trunk/std/haxe/remoting/Proxy.hx?r=3592

You can't replicate with "normal" haxe code. Adding/modifying functionality to haxe.remoting.Proxy for your own class will be difficult, but maybe not impossible.
Check the tutorial for the "equivalent" api implementing code:
http://haxe.org/doc/remoting/proxy

However, something like this is probably possible with macros:
http://haxe.org/manual/macros

Working with macros is challenging right now. There is little in the way of documentation or examples, and the macro feature is still under development. However, it lets you have a some control over the compiler during the compilation process, which can be amazingly useful at times.

good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文