import { throwInternalError } from "./errors.js" export const cached = (thunk: () => T) => { let isCached = false let result: T | undefined return () => { if (!isCached) { result = thunk() isCached = true } return result as T } } export const isThunk = ( value: value ): value is Extract extends never ? value & Thunk : Extract => typeof value === "function" && value.length === 0 export type Thunk = () => ret export type thunkable = t | Thunk export const CompiledFunction = class extends Function { constructor(...args: [string, ...string[]]) { const params = args.slice(0, -1) const body = args.at(-1)! try { super(...params, body) } catch (e) { return throwInternalError( `Encountered an unexpected error while compiling your definition: Message: ${e} Source: (${args.slice(0, -1)}) => { ${args.at(-1)} }` ) } } } as CompiledFunction export type CompiledFunction = new unknown>( ...args: ConstructorParameters ) => f & { apply(thisArg: null, args: Parameters): ReturnType call(thisArg: null, ...args: Parameters): ReturnType }