Community
Participate
Working Groups
Currently, there is no way to specify constructor arguments on enum literals in the JvmModel in analogy to this Java example: public enum FOO { BAR("x"), BAZ("y"); // constructor args private String arg; private Foo(String arg) { this.arg = arg; } }
Here is a work-around. Create your own JvmModelGenerator class: class ExtendedJvmModelGenerator extends JvmModelGenerator { @Inject extension JvmTypeExtensions override generateEumLiteral(JvmEnumerationLiteral it, ITreeAppendable appendable, GeneratorConfig config) { super.generateEnumLiteral(it, appendable, config) compilationStrategy?.apply(appendable) } } Enhance YourLanguageRuntimeModule to get the generator from above injected everywhere: @Override public Class<? extends IGenerator> bindGenerator() { return ExtendedJvmModelGenerator.class; } In YourLanguageJvmModelInferrer set an enum literal's compilationStrategy by calling JvmTypesTypesBuilder's method setInitializer(JvmField, Procedure1<ITreeAppendable>). For example, to generate the enum literal BAR("x") you write @Inject extension JvmTypesBuilder ... members += v.toEnumerationLiteral('BAR') [ initializer = [ append('(') append('"x"') append(')') ] ] Of course, in real life you wouldn't use hard-coded strings, but get strings or XLiterals from the DSL model.
*** Bug 421511 has been marked as a duplicate of this bug. ***
A couple of typos in the workaround: 'generateEumLiteral' for 'generateEnumLiteral' 'bindGenerator' for 'bindIGenerator'