From a33feb54348e6ec9251d858edb1b535c30c7f558 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 19 Dec 2022 18:44:53 -0500 Subject: [PATCH] refactor: Major simplification in providing implementation types --- src/Complex/all.ts | 9 ++++++++- src/Complex/type.ts | 12 ++---------- src/core/Dispatcher.ts | 6 ++++++ src/numbers/all.ts | 9 ++++++++- src/numbers/arithmetic.ts | 3 +-- src/numbers/type.ts | 6 ------ 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/Complex/all.ts b/src/Complex/all.ts index a386daa..9d417b8 100644 --- a/src/Complex/all.ts +++ b/src/Complex/all.ts @@ -1 +1,8 @@ -export * as Complex from './native.js' +import {ForType} from '../core/Dispatcher.js' +import * as Complex from './native.js' + +export {Complex} + +declare module "../core/Dispatcher" { + interface ImplementationTypes extends ForType<'Complex', typeof Complex> {} +} diff --git a/src/Complex/type.ts b/src/Complex/type.ts index 4e9704c..affbedc 100644 --- a/src/Complex/type.ts +++ b/src/Complex/type.ts @@ -1,4 +1,3 @@ -/// import {joinTypes, typeOfDependency, Dependency} from '../core/Dispatcher.js' export type Complex = {re: T; im: T;} @@ -18,13 +17,6 @@ export const Complex_type = { } } -export const complex_1 = (dep: Dependency<'zero', [T]>) => +export const complex_unary = (dep: Dependency<'zero', [T]>) => (t: T) => ({re: t, im: dep.zero(t)}) -export const complex_2 = (t: T, u: T) => ({re: t, im: u}) - -declare module "../core/Dispatcher" { - interface ImplementationTypes { - complex_1_Complex: typeof complex_1 - complex_2_Complex: typeof complex_2 - } -} +export const complex_binary = (t: T, u: T) => ({re: t, im: u}) diff --git a/src/core/Dispatcher.ts b/src/core/Dispatcher.ts index 4b3dfa6..bed8f0d 100644 --- a/src/core/Dispatcher.ts +++ b/src/core/Dispatcher.ts @@ -14,6 +14,12 @@ type Signature = Parameter[] 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 + //dummy implementation for now export function joinTypes(a: TypeName, b: TypeName) { if (a === b) return a diff --git a/src/numbers/all.ts b/src/numbers/all.ts index b71b4c3..5aea220 100644 --- a/src/numbers/all.ts +++ b/src/numbers/all.ts @@ -1 +1,8 @@ -export * as numbers from './native.js' +import {ForType} from '../core/Dispatcher.js' +import * as numbers from './native.js' + +export {numbers} + +declare module "../core/Dispatcher" { + interface ImplementationTypes extends ForType<'numbers', typeof numbers> {} +} diff --git a/src/numbers/arithmetic.ts b/src/numbers/arithmetic.ts index f1facf1..e78d9ec 100644 --- a/src/numbers/arithmetic.ts +++ b/src/numbers/arithmetic.ts @@ -1,6 +1,5 @@ -/// import {configDependency} from '../core/Config.js' -import {Dependency, ImplementationTypes} from '../core/Dispatcher.js' +import {Dependency} from '../core/Dispatcher.js' export const add = (a: number, b: number) => a + b export const unaryMinus = (a: number) => -a diff --git a/src/numbers/type.ts b/src/numbers/type.ts index 4e51f38..67dbd29 100644 --- a/src/numbers/type.ts +++ b/src/numbers/type.ts @@ -5,9 +5,3 @@ export const number_type = { } export const zero = (a: number) => 0 - -declare module "../core/Dispatcher" { - interface ImplementationTypes { - zero_numbers: typeof zero - } -} -- 2.34.1