fix(templates): Enhance the catchall to call the correct specialization

Also enhances type inference in preparation for template types.
This commit is contained in:
Glen Whitney 2022-08-03 09:35:11 -07:00
parent 880efac15b
commit 5dab7d64e7
3 changed files with 174 additions and 43 deletions

View file

@ -8,6 +8,7 @@ import * as complex from '../src/complex/all.mjs'
import * as complexAdd from '../src/complex/add.mjs'
import * as complexNegate from '../src/complex/negate.mjs'
import * as complexComplex from '../src/complex/complex.mjs'
import * as bigintAdd from '../src/bigint/add.mjs'
import * as concreteSubtract from '../src/generic/subtract.concrete.mjs'
import * as genericSubtract from '../src/generic/subtract.mjs'
import extendToComplex from '../src/complex/extendToComplex.mjs'
@ -112,4 +113,25 @@ describe('A custom instance', () => {
math.complex(1n, -3n))
})
it("instantiates templates correctly", () => {
const inst = new PocomathInstance('InstantiateTemplates')
inst.install(numberAdd)
inst.install({typeMerge: {'T,T': ({T}) => (t,u) => 'Merge to ' + T }})
assert.strictEqual(inst.typeMerge(7,6.28), 'Merge to number')
assert.strictEqual(inst.typeMerge(7,6), 'Merge to NumInt')
assert.strictEqual(inst.typeMerge(7.35,6), 'Merge to number')
inst.install(complexAdd)
inst.install(complexComplex)
inst.install(bigintAdd)
assert.strictEqual(
inst.typeMerge(6n, inst.complex(3n, 2n)),
'Merge to GaussianInteger')
assert.strictEqual(
inst.typeMerge(3, inst.complex(4.5,2.1)),
'Merge to Complex')
// The following is the current behavior, since both `3+0i` and `3n + 0ni`
// are Complex, but it is unfortunate and hopefully it will be fixed
// with templates:
assert.strictEqual(inst.typeMerge(3, 3n), 'Merge to Complex')
})
})