我有以下toplevel类:
class Example(shared String first = "default one", shared String second = "default two") {
}
现在,我想使用first
的显式值来实例化此类,但是second
的默认值。
我知道如何通过显式编译代码,使用命名参数:
void instantiateExample(String firstValue) {
Example ex = Example { first = firstValue; };
assert(ex.first == firstValue);
assert(ex.second == "default two");
}
现在,我想做与上面代码相同的事情,但是使用Class<Example, []|[String,String=]>
对象。
答案 0 :(得分:2)
与班级模型是......
Class<Example,[]|[String, String=]> exampleModel = `Example`;
value e1 = exampleModel();
value e2 = exampleModel("foo");
value e3 = exampleModel("foo", "bar");
或者使用类声明它是......
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