From 320cb894f135332fde7e580bc8a83891dfd725b6 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 30 May 2019 14:06:40 +0100 Subject: [PATCH] Friendlier accessors --- implementations/javascript/src/values.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/implementations/javascript/src/values.js b/implementations/javascript/src/values.js index ef4d6d4..ac268f6 100644 --- a/implementations/javascript/src/values.js +++ b/implementations/javascript/src/values.js @@ -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; };