diff --git a/src/Complex/all.ts b/src/Complex/all.ts index 9d417b8..8199908 100644 --- a/src/Complex/all.ts +++ b/src/Complex/all.ts @@ -1,8 +1,9 @@ import {ForType} from '../core/Dispatcher.js' -import * as Complex from './native.js' +import {Complex_type, typeImps} from './type.js' -export {Complex} +export const Complex_bundle = Object.assign({Complex_type}, typeImps()) declare module "../core/Dispatcher" { - interface ImplementationTypes extends ForType<'Complex', typeof Complex> {} + interface ImplementationTypes extends + ForType<'Complex', ReturnType>> {} } diff --git a/src/Complex/type.ts b/src/Complex/type.ts index affbedc..0f1e4bb 100644 --- a/src/Complex/type.ts +++ b/src/Complex/type.ts @@ -17,6 +17,8 @@ export const Complex_type = { } } -export const complex_unary = (dep: Dependency<'zero', [T]>) => - (t: T) => ({re: t, im: dep.zero(t)}) -export const complex_binary = (t: T, u: T) => ({re: t, im: u}) +export const typeImps = () => ({ + complex_unary: (dep: Dependency<'zero', [T]>) => + (t: T) => ({re: t, im: dep.zero(t)}), + complex_binary: (t: T, u: T) => ({re: t, im: u}) +}) diff --git a/src/core/Dispatcher.ts b/src/core/Dispatcher.ts index bed8f0d..79c65d4 100644 --- a/src/core/Dispatcher.ts +++ b/src/core/Dispatcher.ts @@ -11,14 +11,15 @@ type TypeName = string type Parameter = TypeName type Signature = Parameter[] -export interface ImplementationTypes {} +export interface ImplementationTypes {} export type typeOfDependency = {typeOf: (x: unknown) => TypeName} // Helper for collecting implementations // (Really just suffixes the type name onto the keys of exports) -export type ForType = keyof Exports extends string - ? {[K in keyof Exports as `${K}_${T}`]: Exports[K]} - : never +export type ForType = + keyof Exports extends string + ? {[K in keyof Exports as `${K}_${Suffix}`]: Exports[K]} + : never //dummy implementation for now export function joinTypes(a: TypeName, b: TypeName) { @@ -37,6 +38,18 @@ type FinalShape = type BeginsWith = `${Name}${string}` +type ImpNames = keyof ImplementationTypes + +type MatchingParams = + {[K in ImpNames]: K extends BeginsWith + ? FinalShape[K]> extends (...args: Params) => any + ? Parameters[K]>> + : never + : never}[ImpNames] + +export type BestType = + Params extends MatchingParams ? T : unknown + type DependencyTypes = {[K in keyof Ob]: K extends BeginsWith ? FinalShape extends (...args: Params) => any @@ -46,7 +59,7 @@ type DependencyTypes = export type Dependency = {[N in Name]: - DependencyTypes[keyof ImplementationTypes]} + DependencyTypes>, N, Params>[ImpNames]} // Now types used in the Dispatcher class itself diff --git a/src/numbers/all.ts b/src/numbers/all.ts index 5aea220..7e5c454 100644 --- a/src/numbers/all.ts +++ b/src/numbers/all.ts @@ -1,8 +1,13 @@ import {ForType} from '../core/Dispatcher.js' -import * as numbers from './native.js' +import {number_type, typeImps} from './type.js' +import {arithmeticImps} from './arithmetic.js' -export {numbers} +export const numbers_bundle = + Object.assign({number_type}, typeImps(), arithmeticImps()) declare module "../core/Dispatcher" { - interface ImplementationTypes extends ForType<'numbers', typeof numbers> {} + interface ImplementationTypes extends + ForType<'numbers', ReturnType>> {} + interface ImplementationTypes extends + ForType<'numbers', ReturnType>> {} } diff --git a/src/numbers/arithmetic.ts b/src/numbers/arithmetic.ts index e78d9ec..14efcb4 100644 --- a/src/numbers/arithmetic.ts +++ b/src/numbers/arithmetic.ts @@ -1,12 +1,15 @@ import {configDependency} from '../core/Config.js' import {Dependency} from '../core/Dispatcher.js' -export const add = (a: number, b: number) => a + b -export const unaryMinus = (a: number) => -a -export const subtract = (a: number, b: number) => a - b -export const multiply = (a: number, b: number) => a * b -export const divide = (a: number, b: number) => a / b -export const sqrt = +const unaryMinus = (a: number) => -a + +export const arithmeticImps = () => ({ + add: (a: number, b: number) => a + b, + unaryMinus, + subtract: (a: number, b: number) => a - b, + multiply: (a: number, b: number) => a * b, + divide: (a: number, b: number) => a / b, + sqrt: (dep: configDependency & Dependency<'complex', [number, number]>) => { if (dep.config.predictable || !dep.complex) { @@ -18,3 +21,4 @@ export const sqrt = return dep.complex(0, Math.sqrt(unaryMinus(a))) } } +}) diff --git a/src/numbers/type.ts b/src/numbers/type.ts index 67dbd29..fcfc330 100644 --- a/src/numbers/type.ts +++ b/src/numbers/type.ts @@ -4,4 +4,6 @@ export const number_type = { from: {string: s => +s} } -export const zero = (a: number) => 0 +export const typeImps = () => ({ + zero: (a: number) => 0 +})