如何生成多种语言的类
我正在寻找一种方法来生成多种语言(Java、C#、JavaScript)的一些模型类,以保持这些语言之间的一致性。是否有一些工具可以帮助我在一个地方定义模型并在所有三种语言的每次更改后生成它?我正在查看 JetBrains MPS,但它似乎只适用于生成一种语言,甚至在文档中写道,仅推荐一种语言生成器(如果有)。
到目前为止我发现的唯一方法是每种语言的 XML 文件和 XSL 转换,但我正在寻找更方便的方法。
I am looking for a way to generate some model classes to multiple languages (Java, C#, JavaScript) to maintain consistency among these. Is there some tool which will help me define the model in one place and generate it after each change for all three languages? I was looking at JetBrains MPS, but it seems to be useful for generating only one language, it is even written in documentation, that only one language generator (if any) is recommended.
Only way I found so far are XML files and XSL transformation for each language, but I am looking for more convenient way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果“模型”指的是仅保留数据的类,并且需要序列化/反序列化的一致性,则应该考虑 google 协议缓冲区: https://developers.google.com/protocol-buffers/ 或类似的解决方案。
对于更复杂的用途,您可以考虑用 c 语言编写并使用 swig 生成包装器: http://www.swig.org/< /a>,但这是极端的;)
If by "model" you mean classes that only keeps data, and you need consistency for serialization / deserialization, you should consider google protocol buffers: https://developers.google.com/protocol-buffers/ or similar solution.
For more sophisticated uses, you may consider writing in c and generate wrappers with swig: http://www.swig.org/, but is is extreme ;)
从最严格的意义上来说,这是不可能的,因为这些语言的某些特征不能互译。
例如,在 C# 中放入
delegate Method();
(正如您在建模时可能会做的那样),它会立即中断到 Java 的直接转换,因为 Java 没有委托方法的概念。当然,这两种语言都是图灵完备的,所以理论上它们确实会翻译,但翻译者必须:
上面的步骤 2 太复杂了,做起来太复杂了。 (这就是为什么您可以将任何 C# 分发为已编译的 EXE,而不用担心有人窃取您的源代码。)
In the strictest sense, this is impossible, as some characteristics of those languages don't intertranslate.
For example, putting
delegate Method();
in C#, as you might well do when modeling something, it immediately breaks direct conversion to Java as Java has no notion of delegate methods.Sure, both languages are Turing complete, so theoretically they do translate, but the translator would have to:
Step 2 above is way too damn complex to do. (This is why you can distribute any of your C# as compiled EXE and not worry about someone stealing your source code.)