Unifying enums across multiple languages
I have one large project with components in multiple languages that each depend on some of the same enum values. What solutions have you come up with to unify enums across multiple arbitrary langu开发者_Go百科ages? I can think of a few, but I'm looking for the best solution.
(In my implementation, I'm using Php, Java, Javascript, and SQL.)
You can put all of the enums in a text file, then use a code generator to write out the appropriate syntax for each language from that common file so that each component has the enums. Make that text file the authoritative source of information.
You can express the text file in XML but I'd think a tab-delimited flat file would work just fine.
Make them in a format that every language can understand or has a library for. I am using JSON for this at the moment.
Then you can include it with two ways:
- For development: Load it from a file/URL at runtime
- good for small changes you want too see immediately
- slow
- For productive usage: Include it in the files
- using a build script
- fast
- no instant feedback
I would apply the dry principle and using code generator as such you could add anew language easely even if it has not enum natively existing.
精彩评论