feat: Return type annotations (#53)
Provides the infrastructure to allow annotating the return types of functions, and does so for essentially every operation in the system (the only known exceptions being add, multiply, etc., on arbitrarily many arguments). One main infrastructure enhancements are bounded template types, e.g. `T:number` being a template parameter where T can take on the type `number` or any subtype thereof. A main internal enhancement is that base template types are no longer added to the typed universe; rather, there is a secondary, "meta" typed universe where they live. The primary point/purpose of this change is then the necessary search order for implementations can be much better modeled by typed-function's search order, using the `onMismatch` facility to redirect the search from fully instantiated implementations to the generic catchall implementations for each template (these catchalls live in the meta universe). Numerous other small improvements and bugfixes were encountered along the way. Co-authored-by: Glen Whitney <glen@studioinfinity.org> Reviewed-on: #53
This commit is contained in:
parent
207ac4330b
commit
31add66f4c
80 changed files with 1502 additions and 606 deletions
|
@ -20,9 +20,56 @@ describe('The default full pocomath instance "math"', () => {
|
|||
assert.strictEqual(math.typeOf({re: 6.28, im: 2.72}), 'Complex<number>')
|
||||
})
|
||||
|
||||
it('can determine the return types of operations', () => {
|
||||
assert.strictEqual(math.returnTypeOf('negate', 'number'), 'number')
|
||||
assert.strictEqual(math.returnTypeOf('negate', 'NumInt'), 'NumInt')
|
||||
math.negate(math.complex(1.2, 2.8)) // TODO: make this call unnecessary
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('negate', 'Complex<number>'), 'Complex<number>')
|
||||
assert.strictEqual(math.returnTypeOf('add', 'number,number'), 'number')
|
||||
assert.strictEqual(math.returnTypeOf('add', 'NumInt,NumInt'), 'NumInt')
|
||||
assert.strictEqual(math.returnTypeOf('add', 'NumInt,number'), 'number')
|
||||
assert.strictEqual(math.returnTypeOf('add', 'number,NumInt'), 'number')
|
||||
assert.deepStrictEqual( // TODO: ditto
|
||||
math.add(3, math.complex(2.5, 1)), math.complex(5.5, 1))
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('add', 'Complex<number>,NumInt'), 'Complex<number>')
|
||||
// The following is not actually what we want, but the Pocomath type
|
||||
// language isn't powerful enough at this point to capture the true
|
||||
// return type:
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('add', 'number,NumInt,Complex<number>'), 'any')
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('chain', 'bigint'), 'Chain<bigint>')
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('returnTypeOf', 'string,string'), 'string')
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('conjugate', 'bigint'), 'bigint')
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('gcd', 'bigint,bigint'), 'bigint')
|
||||
math.identity(math.fraction(3,5)) // TODO: ditto
|
||||
assert.strictEqual(math.returnTypeOf('identity', 'Fraction'), 'Fraction')
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('quotient', 'bigint,bigint'), 'bigint')
|
||||
math.abs(math.complex(2,1)) //TODO: ditto
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('abs','Complex<NumInt>'), 'number')
|
||||
math.multiply(math.quaternion(1,1,1,1), math.quaternion(1,-1,1,-1)) // dit
|
||||
const quatType = math.returnTypeOf(
|
||||
'quaternion', 'NumInt,NumInt,NumInt,NumInt')
|
||||
assert.strictEqual(quatType, 'Complex<Complex<NumInt>>')
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('multiply', quatType + ',' + quatType), quatType)
|
||||
assert.strictEqual(math.returnTypeOf('isZero', 'NumInt'), 'boolean')
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('roundquotient', 'NumInt,number'), 'NumInt')
|
||||
assert.strictEqual(
|
||||
math.returnTypeOf('factorial', 'NumInt'), 'bigint')
|
||||
})
|
||||
|
||||
it('can subtract numbers', () => {
|
||||
assert.strictEqual(math.subtract(12, 5), 7)
|
||||
//assert.strictEqual(math.subtract(3n, 1.5), 1.5)
|
||||
assert.throws(() => math.subtract(3n, 1.5), 'TypeError')
|
||||
})
|
||||
|
||||
it('can add numbers', () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue