From fa63022656819b602d1013bc66897c7dd19b02d1 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Thu, 22 Dec 2022 11:05:39 -0500 Subject: [PATCH] feat: add generic `square` operation and numeric `unequal` --- src/Complex/type.ts | 13 +++++++++++++ src/all.ts | 1 + src/generic/all.ts | 2 +- src/generic/arithmetic.ts | 26 ++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/Complex/type.ts b/src/Complex/type.ts index a150700..36c040a 100644 --- a/src/Complex/type.ts +++ b/src/Complex/type.ts @@ -30,6 +30,19 @@ export interface ComplexReturn { : Params extends BBinary ? Complex // binary case : never + // alternatively if it seems better; each definition is simpler, but at + // the cost of having two keys here: + // complex_unary: Params extends [infer R] ? Complex : never + // complex_binary: Params extends BBinary ? Complex : never + + // There is actually a subtlety here that complex_unary really only works + // on real types that include their own zero value, so it should really be + // complex_unary: Params extends [infer R] + // ? ImpReturns<'zero', [R]> extends R ? Complex : never + // : never + // and that might actually simplify some of the typings of other operations, + // but we'll leave such fine tuning til later, if we adopt this scheme + zero: Params extends [infer Z] // unary ? Z extends Complex // of a Complex parameter ? ImpReturns<'zero', T> extends T ? Z : never // that has its real 0 diff --git a/src/all.ts b/src/all.ts index 192c7be..e2e83f1 100644 --- a/src/all.ts +++ b/src/all.ts @@ -1,2 +1,3 @@ export * from './numbers/all.js' export * from './Complex/all.js' +export * from './generic/all.js' diff --git a/src/generic/all.ts b/src/generic/all.ts index 40a35e4..1a008ac 100644 --- a/src/generic/all.ts +++ b/src/generic/all.ts @@ -6,5 +6,5 @@ export { generic } declare module "../core/Dispatcher" { interface ReturnTypes - extends ForType<'numbers', GenericReturn> { } + extends ForType<'generic', GenericReturn> { } } diff --git a/src/generic/arithmetic.ts b/src/generic/arithmetic.ts index 5cb4801..770c766 100644 --- a/src/generic/arithmetic.ts +++ b/src/generic/arithmetic.ts @@ -1,13 +1,35 @@ -import { ConservativeUnary, Dependency, ImpType, Signature } from "../core/Dispatcher"; +import { Dependency, ImpType, ImpReturns } from "../core/Dispatcher"; declare module "./type" { interface GenericReturn { // Jos: not sure how to define this or why it is needed - square: Signature + // square: Signature // square: ConservativeUnary // square: Params extends [infer R] // ? R extends number ? UnderlyingReal : never // : never + + // The type of `square` in this interface, instantiated with the type + // Params of a parameter list, needs to be the return type of the + // operation `square` on those parameters. In other words, `square` gives + // a type transformer from the tuple type of its parameters to its return + // type. + // That's how Dispatcher knows what the return type will be in + // `Dependency<'square', [bigint]>`, for example: it instantiates + // GenericReturn with Params equal to [bigint] and then grabs the + // type of the `square` property. Hence we write: + + square: Params extends [infer T] // square only takes 1 arbitrary parameter + ? ImpReturns<'multiply', [T, T]> // and returns whatever multiply does + : never; // otherwise if not a single argument, this implementation + // doesn't handle it + + // If square had more than one implementation in this collection, we could + // either add more conditional clauses to the above type transformer + // as I did in Complex/type.ts for `complex`, or we could have two + // different keys that both start with `square_` and Dispatcher will + // check both (as I have now done in comments in Complex/type.ts and + // verified that also works). } }