sclass RandomAndVary_Record implements RandomAndVary {
F0 makeInstance;
new Map handlers; // key = field name
*(Class theClass, O... params) {
makeInstance = func -> A { nu(theClass) };
addAll(handlers, paramsToMap(params));
}
*(A prototype, O... params) {
makeInstance = func -> A { shallowClone(prototype) };
addAll(handlers, paramsToMap(params));
}
public A randomInstance() {
A a = makeInstance!;
for (S field, RandomAndVary handler : handlers) {
setOpt(a, field, handler.randomInstance());
}
ret a;
}
public A varyInstance(A a) {
a = shallowClone(a);
S field = random(keys(handlers));
RandomAndVary handler = handlers.get(field);
O o = getOpt(a, field);
if (o == null) null;
setOpt(a, field, handler.varyInstance(o));
ret a;
}
}