From 457b9cdc91a9246103804fac65bfd378040781da Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sat, 30 Sep 2023 15:52:14 -0700 Subject: [PATCH] refactor: Possible alternate syntax for reflection --- package.json5 | 2 +- pnpm-lock.yaml | 8 ++++---- src/Complex/arithmetic.ts | 7 ++++--- src/interfaces/type.ts | 4 ++++ src/numbers/arithmetic.ts | 10 ++++++---- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/package.json5 b/package.json5 index 0d15af2..b00af7c 100644 --- a/package.json5 +++ b/package.json5 @@ -23,7 +23,7 @@ url: 'https://code.studioinfinity.org/glen/typocomath.git', }, devDependencies: { - '@types/node': '20.6.2', + '@types/node': '20.8.0', 'cpy-cli': '5.0.0', 'del-cli': '5.1.0', mkdirp: '3.0.1', diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ea413ae..b3f667b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,8 +6,8 @@ settings: devDependencies: '@types/node': - specifier: 20.6.2 - version: 20.6.2 + specifier: 20.8.0 + version: 20.8.0 cpy-cli: specifier: 5.0.0 version: 5.0.0 @@ -79,8 +79,8 @@ packages: resolution: {integrity: sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==} dev: true - /@types/node@20.6.2: - resolution: {integrity: sha512-Y+/1vGBHV/cYk6OI1Na/LHzwnlNCAfU3ZNGrc1LdRe/LAIbdDPTTv/HU3M7yXN448aTVDq3eKRm2cg7iKLb8gw==} + /@types/node@20.8.0: + resolution: {integrity: sha512-LzcWltT83s1bthcvjBmiBvGJiiUe84NWRHkw+ZV6Fr41z2FbIzvc815dk2nQ3RAKMuN2fkenM/z3Xv2QzEpYxQ==} dev: true /@types/normalize-package-data@2.4.1: diff --git a/src/Complex/arithmetic.ts b/src/Complex/arithmetic.ts index 4f78bb5..6cc916d 100644 --- a/src/Complex/arithmetic.ts +++ b/src/Complex/arithmetic.ts @@ -2,7 +2,7 @@ import {Complex} from './type.js' import type { Dependencies, Signature, Returns, RealType, AliasOf } from '../interfaces/type.js' -import {$reflect} from '../interfaces/type.js' +import {$reflecType} from '../interfaces/type.js' declare module "../interfaces/type" { interface Signatures { @@ -74,7 +74,7 @@ export const divide = // and we have to get it straight which operations we need on each type, and // in fact, we need `addReal` on both T and Complex, hence the dependency // with a custom name, not generated via Dependencies<...> -export const sqrt = $reflect!('sqrt', +export const sqrt = (dep: Dependencies<'equal' | 'conservativeSqrt' | 'unaryMinus', RealType> & Dependencies<'zero' | 'complex', T> & Dependencies<'absquare' | 're' | 'divideReal', Complex> @@ -97,4 +97,5 @@ export const sqrt = $reflect!('sqrt', const denomsq = dep.addRR(dep.addRR(myabs, myabs), dep.addRR(r, r)) const denom = dep.conservativeSqrt(denomsq) return dep.divideReal(num, denom) - }) + } +$reflecType!(sqrt) diff --git a/src/interfaces/type.ts b/src/interfaces/type.ts index 2b38374..7e697cc 100644 --- a/src/interfaces/type.ts +++ b/src/interfaces/type.ts @@ -87,3 +87,7 @@ export function $reflect(name: string, expr: Impl) : Impl & { reflectedTyp $$ident!(name).reflectedType = $$typeToString!(true, false, true); return $$ident!(name) } + +export function $reflecType(expr: Impl) { + expr.reflectedType = $$typeToString!(true, false, true); +} diff --git a/src/numbers/arithmetic.ts b/src/numbers/arithmetic.ts index 4b4d2cb..ef92bfe 100644 --- a/src/numbers/arithmetic.ts +++ b/src/numbers/arithmetic.ts @@ -1,6 +1,6 @@ import type {configDependency} from '../core/Config.js' import type {Dependencies, Signature} from '../interfaces/type.js' -import { $reflect } from '../interfaces/type.js' +import { $reflecType } from '../interfaces/type.js' export const add: Signature<'add', number> = (a, b) => a + b export const unaryMinus: Signature<'unaryMinus', number> = a => -a @@ -14,8 +14,9 @@ export const divide: Signature<'divide', number> = (a, b) => a / b const basicSqrt = (a: number) => isNaN(a) ? NaN : Math.sqrt(a) export const conservativeSqrt: Signature<'conservativeSqrt', number> = basicSqrt -export const sqrt = $reflect!('sqrt', - (dep: configDependency & Dependencies<'complex', number>): Signature<'sqrt', number> => { +export const sqrt = + (dep: configDependency + & Dependencies<'complex', number>): Signature<'sqrt', number> => { if (dep.config.predictable || !dep.complex) { return basicSqrt } @@ -24,4 +25,5 @@ export const sqrt = $reflect!('sqrt', if (a >= 0) return Math.sqrt(a) return dep.complex(0, Math.sqrt(unaryMinus(a))) } - }) + } +$reflecType!(sqrt)