feat: Template operations #41

Merged
glen merged 10 commits from template_operations into main 2022-08-01 10:09:32 +00:00
Showing only changes of commit 7bbdc049ce - Show all commits

View File

@ -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})