Friendlier accessors

This commit is contained in:
Tony Garnock-Jones 2019-05-30 14:06:40 +01:00
parent 921fe7dfd2
commit 320cb894f1
1 changed files with 9 additions and 0 deletions

View File

@ -373,6 +373,15 @@ Record.makeBasicConstructor = function (label, fieldNames) {
ctor.isClassOf = (v) => ((v instanceof Record) &&
is(label, v.label) &&
v.fields.size === arity);
fieldNames.forEach((name, i) => {
ctor['_'+name] = function (r) {
if (!ctor.isClassOf(r)) {
throw new Error("Record: attempt to retrieve field "+label+"."+name+
" from non-"+label+": "+(r && r.toString()));
}
return r.get(i);
};
});
return ctor;
};