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

Merged
glen merged 1 commits from type_registration into main 2023-08-26 02:16:00 +00:00
3 changed files with 15 additions and 11 deletions
Showing only changes of commit 44667a865e - Show all commits

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 }