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.
This commit is contained in:
Glen Whitney 2023-08-25 19:11:00 -07:00
parent 327a9385ed
commit 44667a865e
3 changed files with 15 additions and 11 deletions

View File

@ -6,6 +6,7 @@ import type {
export type Complex<T> = { re: T; im: T; } export type Complex<T> = { re: T; im: T; }
export const Complex_type = { export const Complex_type = {
name: 'Complex', // just until we have reflection to tell us
test: <T>(dep: { testT: (z: unknown) => z is T }) => test: <T>(dep: { testT: (z: unknown) => z is T }) =>
(z: unknown): z is Complex<T> => (z: unknown): z is Complex<T> =>
typeof z === 'object' && z != null && 're' in z && 'im' in z typeof z === 'object' && z != null && 're' in z && 'im' in z

View File

@ -25,6 +25,7 @@ export function joinTypes(a: TypeName, b: TypeName) {
// Now types used in the Dispatcher class itself // Now types used in the Dispatcher class itself
type TypeSpecification = { type TypeSpecification = {
name: string, // just until we get reflection, then we can remove this property
before?: TypeName[], before?: TypeName[],
test: ((x: unknown) => boolean) test: ((x: unknown) => boolean)
| ((d: DependenciesType) => (x: unknown) => boolean), | ((d: DependenciesType) => (x: unknown) => boolean),
@ -53,23 +54,24 @@ export class Dispatcher {
//TODO: implement me //TODO: implement me
} }
constructor(collection: SpecificationsGroup) { constructor(collection: SpecificationsGroup) {
const implementations = []
for (const key in collection) { for (const key in collection) {
console.log('Working on', key) console.log('Working on', key)
for (const identifier in collection[key]) { for (const identifier in collection[key]) {
console.log('Handling', key, ':', identifier) const item = collection[key][identifier]
const parts = identifier.split('_') if (typeof item === 'function') {
if (parts[parts.length - 1] === 'type') { implementations.push([key, identifier, item])
parts.pop()
const name = parts.join('_')
this.installType(
name, collection[key][identifier] as TypeSpecification)
} else { } else {
const name = parts[0] console.log('Handling type', key, ':', identifier)
this.installSpecification( this.installType(
name, ['dunno'], 'unsure', {}, item.name, collection[key][identifier] as TypeSpecification)
collection[key][identifier] as Function)
} }
} }
} }
for (const trio of implementations) {
const [k, id, imp] = trio
console.log('Handling implementation', id, 'from', k)
this.installSpecification(id, ['dunno'], 'unsure', {}, imp)
}
} }
} }

View File

@ -1,6 +1,7 @@
import type { Signature } from '../interfaces/type.js' import type { Signature } from '../interfaces/type.js'
export const number_type = { export const number_type = {
name: 'number', // just until we have reflection to tell us
before: ['Complex'], before: ['Complex'],
test: (n: unknown): n is number => typeof n === 'number', test: (n: unknown): n is number => typeof n === 'number',
from: { string: (s: string) => +s } from: { string: (s: string) => +s }