feat: add type reflection via ts-macros

This commit is contained in:
Glen Whitney 2024-09-29 22:10:09 -07:00
parent f575582879
commit a0324e5f2b
11 changed files with 215 additions and 14 deletions

View file

@ -1,2 +1,2 @@
export * as type_data from './type'
export * as arithmetic_functions from './arithmetic'
export * as type from './type'
export * as arithmetic from './arithmetic'

View file

@ -2,7 +2,7 @@ import {Complex} from './type.js'
import {implementations, commonSpecs} from '@/core/Dispatcher'
import type {RawDependencies} from '@/core/Dispatcher'
import {commonSignature, RealType} from '@/interfaces/type'
import {commonSignature, RealType, $reflect} from '@/interfaces/type'
import type {CommonSignature, CommonReturn} from '@/interfaces/type'
// Narrowly typed signature selectors, for the operations we need to use
@ -10,7 +10,7 @@ import type {CommonSignature, CommonReturn} from '@/interfaces/type'
const add = 'add' as const
const divide = 'divide' as const
export default function <T>() {
export function common<T>() {
const baseSignature = commonSpecs<T>()
const withComplex
= <RD extends RawDependencies>(rd: RD) => baseSignature({
@ -150,3 +150,5 @@ export function mixed<T>() {
})
.ship()
}
$reflect!([common, mixed])

View file

@ -1,5 +1,6 @@
import {implementations, commonSpecs} from '@/core/Dispatcher'
import {joinTypes} from '@/core/type'
import {$reflect} from '@/interfaces/type'
import type {
CommonInterface, CommonSignature, NaNType, OneType, ZeroType
@ -50,7 +51,7 @@ export function lift<T>() {
}).ship()
}
export default function <T>() {
export function common<T>() {
const baseSignature = commonSpecs<T>()
return implementations<CommonSignature<Complex<T>>>()
@ -69,3 +70,5 @@ export default function <T>() {
})
.ship()
}
$reflect!([common, lift])

View file

@ -1,3 +1,5 @@
import {$$typeToString} from 'ts-macros'
import {Configuration} from '@/core/Configuration'
/* First some type utilities: */
@ -114,3 +116,11 @@ export type Dependency<T, Aux = T> = {
export type Dependencies<T, Names extends SignatureKey<T>, Aux = T> =
Pick<CommonSignature<T, Aux>, Names>
// Macro for type reflection:
export function $reflect<ImplTuple>(tup: ImplTuple) {
+[[tup], <T>(elt: T) => {
(elt as {reflectedType: string}).reflectedType
= $$typeToString!<T>(true, false, true);
}]
}

View file

@ -1,2 +1,2 @@
export * as type_data from './type'
export * as arithmetic_functions from './arithmetic'
export * as type from './type'
export * as arithmetic from './arithmetic'

View file

@ -1,9 +1,10 @@
import {implementations} from '@/core/Dispatcher'
import {$reflect} from '@/interfaces/type'
import type {CommonSignature} from '@/interfaces/type'
const conservativeSqrt = (a: number) => isNaN(a) ? NaN : Math.sqrt(a)
export default implementations<CommonSignature<number>>()
export const common = implementations<CommonSignature<number>>()
.independent({
add: (a, b) => a + b,
unaryMinus: a => -a,
@ -24,3 +25,5 @@ export default implementations<CommonSignature<number>>()
}
}})
.ship()
$reflect!([common])

View file

@ -1,5 +1,6 @@
import type {Complex} from '@/Complex/type'
import {implementations} from '@/core/Dispatcher'
import {$reflect} from '@/interfaces/type'
import type {CommonSignature} from '@/interfaces/type'
export const number_type = {
@ -22,10 +23,12 @@ declare module "@/interfaces/type" {
}
}
export default implementations<CommonSignature<number>>()
export const common = implementations<CommonSignature<number>>()
.independent({
zero: a => 0,
one: a => 1,
nan: a => NaN,
re: a => a
}).ship()
$reflect!([common])