From 44667a865e89893414a938e5afb1ec12fc9fd857 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Fri, 25 Aug 2023 19:11:00 -0700 Subject: [PATCH] fix: Add types first and distinguish them semantically. Rather than via some format on the name of the identifier, this commit changes the construction of Dispatcher to assume that functions are implementations and other objects are type specifiers. Also installs all types first, before any implementations. Resolves #3. Resolves #12. --- src/Complex/type.ts | 1 + src/core/Dispatcher.ts | 24 +++++++++++++----------- src/numbers/type.ts | 1 + 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Complex/type.ts b/src/Complex/type.ts index 50a1314..10f76b6 100644 --- a/src/Complex/type.ts +++ b/src/Complex/type.ts @@ -6,6 +6,7 @@ import type { export type Complex = { re: T; im: T; } export const Complex_type = { + name: 'Complex', // just until we have reflection to tell us test: (dep: { testT: (z: unknown) => z is T }) => (z: unknown): z is Complex => typeof z === 'object' && z != null && 're' in z && 'im' in z diff --git a/src/core/Dispatcher.ts b/src/core/Dispatcher.ts index def1bd3..ec5491f 100644 --- a/src/core/Dispatcher.ts +++ b/src/core/Dispatcher.ts @@ -25,6 +25,7 @@ export function joinTypes(a: TypeName, b: TypeName) { // Now types used in the Dispatcher class itself type TypeSpecification = { + name: string, // just until we get reflection, then we can remove this property before?: TypeName[], test: ((x: unknown) => boolean) | ((d: DependenciesType) => (x: unknown) => boolean), @@ -53,23 +54,24 @@ export class Dispatcher { //TODO: implement me } constructor(collection: SpecificationsGroup) { + const implementations = [] for (const key in collection) { console.log('Working on', key) for (const identifier in collection[key]) { - console.log('Handling', key, ':', identifier) - const parts = identifier.split('_') - if (parts[parts.length - 1] === 'type') { - parts.pop() - const name = parts.join('_') - this.installType( - name, collection[key][identifier] as TypeSpecification) + const item = collection[key][identifier] + if (typeof item === 'function') { + implementations.push([key, identifier, item]) } else { - const name = parts[0] - this.installSpecification( - name, ['dunno'], 'unsure', {}, - collection[key][identifier] as Function) + console.log('Handling type', key, ':', identifier) + this.installType( + item.name, collection[key][identifier] as TypeSpecification) } } } + for (const trio of implementations) { + const [k, id, imp] = trio + console.log('Handling implementation', id, 'from', k) + this.installSpecification(id, ['dunno'], 'unsure', {}, imp) + } } } diff --git a/src/numbers/type.ts b/src/numbers/type.ts index 48fb95e..f234a8f 100644 --- a/src/numbers/type.ts +++ b/src/numbers/type.ts @@ -1,6 +1,7 @@ import type { Signature } from '../interfaces/type.js' export const number_type = { + name: 'number', // just until we have reflection to tell us before: ['Complex'], test: (n: unknown): n is number => typeof n === 'number', from: { string: (s: string) => +s } -- 2.34.1