开发者

How to generate classes for multiple languages

开发者 https://www.devze.com 2023-03-11 21:03 出处:网络
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 pl

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 langua开发者_如何学运维ge 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.


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


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

0

精彩评论

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