How to instantiate a class using an explicit first, and a default second argument, via the meta model?

StackOverflow https://stackoverflow.com/questions/23282764

  •  09-07-2023
  •  | 
  •  

Question

I have the following toplevel class:

class Example(shared String first = "default one", shared String second = "default two") {
}

Now, I want to instantiate this class using an explicit value for first, but the default value for second.

I know how to do this via explicitly compiled code, by using named arguments:

void instantiateExample(String firstValue) {
    Example ex = Example { first = firstValue; };
    assert(ex.first == firstValue);
    assert(ex.second == "default two");
}

Now, I would like to do the same thing as the above code, but by using the Class<Example, []|[String,String=]> object.

Was it helpful?

Solution

with class model it is ...

Class<Example,[]|[String, String=]> exampleModel = `Example`;
value e1 = exampleModel();
value e2 = exampleModel("foo");
value e3 = exampleModel("foo", "bar");

or with class declaration it is ...

ClassDeclaration exampleDeclaration = `class Example`;
assert(is Example e1 = exampleDeclaration.instantiate());
assert(is Example e2 = exampleDeclaration.instantiate([], "foo"));
assert(is Example e3 = exampleDeclaration.instantiate([], "foo", "bar"));

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top