From 7bbdc049ce20f62f0da1858a2d9ed590e8c66e6a Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sun, 31 Jul 2022 21:08:28 -0700 Subject: [PATCH] refactor: add stub for patch to generic template behavior --- src/core/PocomathInstance.mjs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/core/PocomathInstance.mjs b/src/core/PocomathInstance.mjs index 4fc7b5b..ba502d0 100644 --- a/src/core/PocomathInstance.mjs +++ b/src/core/PocomathInstance.mjs @@ -434,9 +434,35 @@ export default class PocomathInstance { substituteInSig(rawSignature, theTemplateParam, instType) this._addTFimplementation(tf_imps, signature, behavior, instType) } - /* Now add the catchall signature; for now, punting on the behavior */ + /* Now add the catchall signature */ const signature = substituteInSig(rawSignature, theTemplateParam, 'any') - this._addTFimplementation(tf_imps, signature, behavior) + /* The catchall signature has to detect the actual type of the call + * and add the new instantiations + */ + const argTypes = rawSignature.split(',') + let exemplar = -1 + for (let i = 0; i < argTypes.length; ++i) { + const argType = argTypes[i].trim() + if (argType === theTemplateParam) { + exemplar = i + break + } + } + if (exemplar < 0) { + throw new SyntaxError( + `Cannot find template parameter in ${rawSignature}`) + } + const self = this + const patch = (refs) => { + const original = behavior.does(refs) + return (...args) => { + const example = args[exemplar] + console.log('Have to match template to', example) + return original(...args) + } + } + this._addTFimplementation( + tf_imps, signature, {uses: behavior.uses, does: patch}) } const tf = this._typed(name, tf_imps) Object.defineProperty(this, name, {configurable: true, value: tf})