fix: Add types first and distinguish them semantically. (#15)

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.

Reviewed-on: #15
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
This commit is contained in:
Glen Whitney 2023-08-26 02:15:59 +00:00 committed by Glen Whitney
parent 327a9385ed
commit 180e772dab
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 const Complex_type = {
name: 'Complex', // just until we have reflection to tell us
test: <T>(dep: { testT: (z: unknown) => z is T }) =>
(z: unknown): z is Complex<T> =>
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
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)
}
}
}

View File

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