feat: add generic square operation and numeric unequal

This commit is contained in:
Glen Whitney 2022-12-22 11:05:39 -05:00
parent 22f114d7f9
commit fa63022656
4 changed files with 39 additions and 3 deletions

View File

@ -30,6 +30,19 @@ export interface ComplexReturn<Params> {
: Params extends BBinary<infer B> ? Complex<B> // binary case : Params extends BBinary<infer B> ? Complex<B> // binary case
: never : 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<R> : never
// complex_binary: Params extends BBinary<infer R> ? Complex<R> : 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<R> : 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 zero: Params extends [infer Z] // unary
? Z extends Complex<infer T> // of a Complex parameter ? Z extends Complex<infer T> // of a Complex parameter
? ImpReturns<'zero', T> extends T ? Z : never // that has its real 0 ? ImpReturns<'zero', T> extends T ? Z : never // that has its real 0

View File

@ -1,2 +1,3 @@
export * from './numbers/all.js' export * from './numbers/all.js'
export * from './Complex/all.js' export * from './Complex/all.js'
export * from './generic/all.js'

View File

@ -6,5 +6,5 @@ export { generic }
declare module "../core/Dispatcher" { declare module "../core/Dispatcher" {
interface ReturnTypes<Params> interface ReturnTypes<Params>
extends ForType<'numbers', GenericReturn<Params>> { } extends ForType<'generic', GenericReturn<Params>> { }
} }

View File

@ -1,13 +1,35 @@
import { ConservativeUnary, Dependency, ImpType, Signature } from "../core/Dispatcher"; import { Dependency, ImpType, ImpReturns } from "../core/Dispatcher";
declare module "./type" { declare module "./type" {
interface GenericReturn<Params> { interface GenericReturn<Params> {
// Jos: not sure how to define this or why it is needed // Jos: not sure how to define this or why it is needed
square: Signature<Params, [T], T> // square: Signature<Params, [T], T>
// square: ConservativeUnary<Params, T> // square: ConservativeUnary<Params, T>
// square: Params extends [infer R] // square: Params extends [infer R]
// ? R extends number ? UnderlyingReal<R> : never // ? R extends number ? UnderlyingReal<R> : never
// : 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).
} }
} }