如何生成多种语言的类

发布于 2024-11-13 22:08:36 字数 279 浏览 9 评论 0原文

我正在寻找一种方法来生成多种语言(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 技术交流群。

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

发布评论

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

评论(2

拿命拼未来 2024-11-20 22:08:36

如果“模型”指的是仅保留数据的类,并且需要序列化/反序列化的一致性,则应该考虑 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 ;)

你对谁都笑 2024-11-20 22:08:36

从最严格的意义上来说,这是不可能的,因为这些语言的某些特征不能互译。

例如,在 C# 中放入 delegate Method();(正如您在建模时可能会做的那样),它会立即中断到 Java 的直接转换,因为 Java 没有委托方法的概念。

当然,这两种语言都是图灵完备的,所以理论上它们确实会翻译,但翻译者必须:

  1. 将输入语言编译成足够低级的语言,以便输入并且输出语言以某种方式共享该语言作为源(可能是汇编器)。
  2. 将该低级语言反编译为输出语言中的许多等效形式之一。

上面的步骤 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:

  1. Compile the input language into a low-level enough language that both the input and the output language somehow share that language as an origin (might be Assembler).
  2. Decompile that low-level language back up into one of the many equivalent forms in the output language.

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

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