refactor: Major simplification in providing implementation types (#2)

This PR is an effort to address #1. It removes all boilerplate
  from individual implementation files, and moves it into
  a small, fixed section in the single `all.ts` module for each
  type that collects up all of the implementations relating
  to that type.

Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Reviewed-on: #2
This commit is contained in:
Glen Whitney 2022-12-19 23:50:24 +00:00
parent 29bcab1639
commit 3fa216d1f4
6 changed files with 25 additions and 20 deletions

View File

@ -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> {}
}

View File

@ -1,4 +1,3 @@
/// <reference path="../numbers/type.ts">
import {joinTypes, typeOfDependency, Dependency} from '../core/Dispatcher.js'
export type Complex<T> = {re: T; im: T;}
@ -18,13 +17,6 @@ export const Complex_type = {
}
}
export const complex_1 = <T>(dep: Dependency<'zero', [T]>) =>
export const complex_unary = <T>(dep: Dependency<'zero', [T]>) =>
(t: T) => ({re: t, im: dep.zero(t)})
export const complex_2 = <T>(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: T, u: T) => ({re: t, im: u})

View File

@ -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<T extends string, Exports> = 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

View File

@ -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> {}
}

View File

@ -1,6 +1,5 @@
/// <reference path="../Complex/type.ts">
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

View File

@ -5,9 +5,3 @@ export const number_type = {
}
export const zero = (a: number) => 0
declare module "../core/Dispatcher" {
interface ImplementationTypes {
zero_numbers: typeof zero
}
}