From 47370cec9ee6f16e4d7615b5984bb2892aeca333 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sat, 26 Apr 2025 00:20:30 -0700 Subject: [PATCH] feat: Index behaviors by return typing strategy * Adds the concept of, and options for, the return typing strategy * adds the strategy at the beginning of every behavior index * adds the strategy as an additional argument to resolve * No actual use of return type strategy so far * Sets up eslint * Fixes eslint errors --- etc/eslint.config.mjs | 14 + package.json5 | 4 + pnpm-lock.yaml | 479 +++++++++++++++++++++++ src/boolean/type.js | 2 +- src/complex/Complex.js | 2 +- src/core/Type.js | 16 +- src/core/TypeDispatcher.js | 102 +++-- src/core/TypePatterns.js | 2 +- src/core/__test__/Type.spec.js | 5 +- src/core/__test__/TypeDispatcher.spec.js | 13 +- src/core/config.js | 11 + src/core/type.js | 3 +- src/generic/all.js | 1 - src/generic/config.js | 5 - src/number/relational.js | 1 - src/number/utils.js | 1 - 16 files changed, 602 insertions(+), 59 deletions(-) create mode 100644 etc/eslint.config.mjs create mode 100644 src/core/config.js delete mode 100644 src/generic/config.js diff --git a/etc/eslint.config.mjs b/etc/eslint.config.mjs new file mode 100644 index 0000000..d00cbda --- /dev/null +++ b/etc/eslint.config.mjs @@ -0,0 +1,14 @@ +import {defineConfig} from 'eslint/config' +import js from '@eslint/js' +import globals from 'globals' + +export default defineConfig([{ + files: ['**/*.js'], + plugins: {js}, + languageOptions: {globals: {...globals.mocha, ...globals.node}}, + extends: ['js/recommended'], + rules: { + 'no-unused-vars': ['error', {argsIgnorePattern: '^_'}], + 'no-empty': ['error', {allowEmptyCatch: true}], + } +}]) diff --git a/package.json5 b/package.json5 index 50e6463..82af637 100644 --- a/package.json5 +++ b/package.json5 @@ -4,6 +4,7 @@ description: 'Sequel to mathjs as prototype for mathjs overhaul', scripts: { test: 'mocha ./**/*.spec.js', + lint: 'eslint -c etc/eslint.config.mjs', }, keywords: [ 'math', @@ -16,6 +17,9 @@ url: 'https://code.studioinfinity.org/glen/nanomath.git', }, devDependencies: { + '@eslint/js': '^9.25.1', + eslint: '^9.25.1', + globals: '^16.0.0', mocha: '^11.1.0', }, dependencies: { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2704e5b..d898d03 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,12 +12,79 @@ importers: specifier: ^2.1.3 version: 2.1.3 devDependencies: + '@eslint/js': + specifier: ^9.25.1 + version: 9.25.1 + eslint: + specifier: ^9.25.1 + version: 9.25.1 + globals: + specifier: ^16.0.0 + version: 16.0.0 mocha: specifier: ^11.1.0 version: 11.1.0 packages: + '@eslint-community/eslint-utils@4.6.1': + resolution: {integrity: sha512-KTsJMmobmbrFLe3LDh0PC2FXpcSYJt/MLjlkh/9LEnmKYLSYmT/0EW9JWANjeoemiuZrmogti0tW5Ch+qNUYDw==} + engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} + peerDependencies: + eslint: ^6.0.0 || ^7.0.0 || >=8.0.0 + + '@eslint-community/regexpp@4.12.1': + resolution: {integrity: sha512-CCZCDJuduB9OUkFkY2IgppNZMi2lBQgD2qzwXkEia16cge2pijY/aXi96CJMquDMn3nJdlPV1A5KrJEXwfLNzQ==} + engines: {node: ^12.0.0 || ^14.0.0 || >=16.0.0} + + '@eslint/config-array@0.20.0': + resolution: {integrity: sha512-fxlS1kkIjx8+vy2SjuCB94q3htSNrufYTXubwiBFeaQHbH6Ipi43gFJq2zCMt6PHhImH3Xmr0NksKDvchWlpQQ==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + + '@eslint/config-helpers@0.2.1': + resolution: {integrity: sha512-RI17tsD2frtDu/3dmI7QRrD4bedNKPM08ziRYaC5AhkGrzIAJelm9kJU1TznK+apx6V+cqRz8tfpEeG3oIyjxw==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + + '@eslint/core@0.13.0': + resolution: {integrity: sha512-yfkgDw1KR66rkT5A8ci4irzDysN7FRpq3ttJolR88OqQikAWqwA8j5VZyas+vjyBNFIJ7MfybJ9plMILI2UrCw==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + + '@eslint/eslintrc@3.3.1': + resolution: {integrity: sha512-gtF186CXhIl1p4pJNGZw8Yc6RlshoePRvE0X91oPGb3vZ8pM3qOS9W9NGPat9LziaBV7XrJWGylNQXkGcnM3IQ==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + + '@eslint/js@9.25.1': + resolution: {integrity: sha512-dEIwmjntEx8u3Uvv+kr3PDeeArL8Hw07H9kyYxCjnM9pBjfEhk6uLXSchxxzgiwtRhhzVzqmUSDFBOi1TuZ7qg==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + + '@eslint/object-schema@2.1.6': + resolution: {integrity: sha512-RBMg5FRL0I0gs51M/guSAj5/e14VQ4tpZnQNWwuDT66P14I43ItmPfIZRhO9fUVIPOAQXU47atlywZ/czoqFPA==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + + '@eslint/plugin-kit@0.2.8': + resolution: {integrity: sha512-ZAoA40rNMPwSm+AeHpCq8STiNAwzWLJuP8Xv4CHIc9wv/PSuExjMrmjfYNj682vW0OOiZ1HKxzvjQr9XZIisQA==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + + '@humanfs/core@0.19.1': + resolution: {integrity: sha512-5DyQ4+1JEUzejeK1JGICcideyfUbGixgS9jNgex5nqkW+cY7WZhxBigmieN5Qnw9ZosSNVC9KQKyb+GUaGyKUA==} + engines: {node: '>=18.18.0'} + + '@humanfs/node@0.16.6': + resolution: {integrity: sha512-YuI2ZHQL78Q5HbhDiBA1X4LmYdXCKCMQIfw0pw7piHJwyREFebJUvrQN4cMssyES6x+vfUbx1CIpaQUKYdQZOw==} + engines: {node: '>=18.18.0'} + + '@humanwhocodes/module-importer@1.0.1': + resolution: {integrity: sha512-bxveV4V8v5Yb4ncFTT3rPSgZBOpCkjfK0y4oVVVJwIuDVBRMDXrPyXRL988i5ap9m9bnyEEjWfm5WkBmtffLfA==} + engines: {node: '>=12.22'} + + '@humanwhocodes/retry@0.3.1': + resolution: {integrity: sha512-JBxkERygn7Bv/GbN5Rv8Ul6LVknS+5Bp6RgDC/O8gEBU/yeH5Ui5C/OlWrTb6qct7LjjfT6Re2NxB0ln0yYybA==} + engines: {node: '>=18.18'} + + '@humanwhocodes/retry@0.4.2': + resolution: {integrity: sha512-xeO57FpIu4p1Ri3Jq/EXq4ClRm86dVF2z/+kvFnyqVYRavTZmaFaUBbWCOuuTh0o/g7DSsk6kc2vrS4Vl5oPOQ==} + engines: {node: '>=18.18'} + '@isaacs/cliui@8.0.2': resolution: {integrity: sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==} engines: {node: '>=12'} @@ -26,6 +93,25 @@ packages: resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==} engines: {node: '>=14'} + '@types/estree@1.0.7': + resolution: {integrity: sha512-w28IoSUCJpidD/TGviZwwMJckNESJZXFu7NBZ5YJ4mEUnNraUn9Pm8HSZm/jDF1pDWYKspWE7oVphigUPRakIQ==} + + '@types/json-schema@7.0.15': + resolution: {integrity: sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==} + + acorn-jsx@5.3.2: + resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==} + peerDependencies: + acorn: ^6.0.0 || ^7.0.0 || ^8.0.0 + + acorn@8.14.1: + resolution: {integrity: sha512-OvQ/2pUDKmgfCg++xsTX1wGxfTaszcHVcTctW4UJB4hibJx2HXxxO5UmVgyjMa+ZDsiaf5wWLXYpRWMmBI0QHg==} + engines: {node: '>=0.4.0'} + hasBin: true + + ajv@6.12.6: + resolution: {integrity: sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==} + ansi-colors@4.1.3: resolution: {integrity: sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==} engines: {node: '>=6'} @@ -63,6 +149,9 @@ packages: resolution: {integrity: sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==} engines: {node: '>=8'} + brace-expansion@1.1.11: + resolution: {integrity: sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==} + brace-expansion@2.0.1: resolution: {integrity: sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==} @@ -73,6 +162,10 @@ packages: browser-stdout@1.3.1: resolution: {integrity: sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==} + callsites@3.1.0: + resolution: {integrity: sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==} + engines: {node: '>=6'} + camelcase@6.3.0: resolution: {integrity: sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==} engines: {node: '>=10'} @@ -96,6 +189,9 @@ packages: color-name@1.1.4: resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==} + concat-map@0.0.1: + resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==} + cross-spawn@7.0.6: resolution: {integrity: sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==} engines: {node: '>= 8'} @@ -113,6 +209,9 @@ packages: resolution: {integrity: sha512-9iE1PgSik9HeIIw2JO94IidnE3eBoQrFJ3w7sFuzSX4DpmZ3v5sZpUiV5Swcf6mQEF+Y0ru8Neo+p+nyh2J+hQ==} engines: {node: '>=10'} + deep-is@0.1.4: + resolution: {integrity: sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==} + diff@5.2.0: resolution: {integrity: sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==} engines: {node: '>=0.3.1'} @@ -134,6 +233,61 @@ packages: resolution: {integrity: sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==} engines: {node: '>=10'} + eslint-scope@8.3.0: + resolution: {integrity: sha512-pUNxi75F8MJ/GdeKtVLSbYg4ZI34J6C0C7sbL4YOp2exGwen7ZsuBqKzUhXd0qMQ362yET3z+uPwKeg/0C2XCQ==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + + eslint-visitor-keys@3.4.3: + resolution: {integrity: sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag==} + engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} + + eslint-visitor-keys@4.2.0: + resolution: {integrity: sha512-UyLnSehNt62FFhSwjZlHmeokpRK59rcz29j+F1/aDgbkbRTk7wIc9XzdoasMUbRNKDM0qQt/+BJ4BrpFeABemw==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + + eslint@9.25.1: + resolution: {integrity: sha512-E6Mtz9oGQWDCpV12319d59n4tx9zOTXSTmc8BLVxBx+G/0RdM5MvEEJLU9c0+aleoePYYgVTOsRblx433qmhWQ==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + hasBin: true + peerDependencies: + jiti: '*' + peerDependenciesMeta: + jiti: + optional: true + + espree@10.3.0: + resolution: {integrity: sha512-0QYC8b24HWY8zjRnDTL6RiHfDbAWn63qb4LMj1Z4b076A4une81+z03Kg7l7mn/48PUTqoLptSXez8oknU8Clg==} + engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + + esquery@1.6.0: + resolution: {integrity: sha512-ca9pw9fomFcKPvFLXhBKUK90ZvGibiGOvRJNbjljY7s7uq/5YO4BOzcYtJqExdx99rF6aAcnRxHmcUHcz6sQsg==} + engines: {node: '>=0.10'} + + esrecurse@4.3.0: + resolution: {integrity: sha512-KmfKL3b6G+RXvP8N1vr3Tq1kL/oCFgn2NYXEtqP8/L3pKapUA4G8cFVaoF3SU323CD4XypR/ffioHmkti6/Tag==} + engines: {node: '>=4.0'} + + estraverse@5.3.0: + resolution: {integrity: sha512-MMdARuVEQziNTeJD8DgMqmhwR11BRQ/cBP+pLtYdSTnf3MIO8fFeiINEbX36ZdNlfU/7A9f3gUw49B3oQsvwBA==} + engines: {node: '>=4.0'} + + esutils@2.0.3: + resolution: {integrity: sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==} + engines: {node: '>=0.10.0'} + + fast-deep-equal@3.1.3: + resolution: {integrity: sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==} + + fast-json-stable-stringify@2.1.0: + resolution: {integrity: sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==} + + fast-levenshtein@2.0.6: + resolution: {integrity: sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==} + + file-entry-cache@8.0.0: + resolution: {integrity: sha512-XXTUwCvisa5oacNGRP9SfNtYBNAMi+RPwBFmblZEF7N7swHYQS6/Zfk7SRwx4D5j3CH211YNRco1DEMNVfZCnQ==} + engines: {node: '>=16.0.0'} + fill-range@7.1.1: resolution: {integrity: sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==} engines: {node: '>=8'} @@ -142,10 +296,17 @@ packages: resolution: {integrity: sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==} engines: {node: '>=10'} + flat-cache@4.0.1: + resolution: {integrity: sha512-f7ccFPK3SXFHpx15UIGyRJ/FJQctuKZ0zVuN3frBo4HnK3cay9VEW0R6yPYFHC0AgqhukPzKjq22t5DmAyqGyw==} + engines: {node: '>=16'} + flat@5.0.2: resolution: {integrity: sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ==} hasBin: true + flatted@3.3.3: + resolution: {integrity: sha512-GX+ysw4PBCz0PzosHDepZGANEuFCMLrnRTiEy9McGjmkCQYwRq4A/X786G/fjM/+OjsWSU1ZrY5qyARZmO/uwg==} + foreground-child@3.3.1: resolution: {integrity: sha512-gIXjKqtFuWEgzFRJA9WCQeSJLZDjgJUOMCMzxtvFq/37KojM1BFGufqsCy0r4qSQmYLsZYMeyRqzIWOMup03sw==} engines: {node: '>=14'} @@ -163,10 +324,22 @@ packages: resolution: {integrity: sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==} engines: {node: '>= 6'} + glob-parent@6.0.2: + resolution: {integrity: sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==} + engines: {node: '>=10.13.0'} + glob@10.4.5: resolution: {integrity: sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg==} hasBin: true + globals@14.0.0: + resolution: {integrity: sha512-oahGvuMGQlPw/ivIYBjVSrWAfWLBeku5tpPE2fOPLi+WHffIWbuh2tCjhyQhTBPMf5E9jDEH4FOmTYgYwbKwtQ==} + engines: {node: '>=18'} + + globals@16.0.0: + resolution: {integrity: sha512-iInW14XItCXET01CQFqudPOWP2jYMl7T+QRQT+UNcR/iQncN/F0UNpgd76iFkBPgNQb4+X3LV9tLJYzwh+Gl3A==} + engines: {node: '>=18'} + has-flag@4.0.0: resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==} engines: {node: '>=8'} @@ -175,6 +348,18 @@ packages: resolution: {integrity: sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==} hasBin: true + ignore@5.3.2: + resolution: {integrity: sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==} + engines: {node: '>= 4'} + + import-fresh@3.3.1: + resolution: {integrity: sha512-TR3KfrTZTYLPB6jUjfx6MF9WcWrHL9su5TObK4ZkYgBdWKPOFoSoQIdEuTuR82pmtxH2spWG9h6etwfr1pLBqQ==} + engines: {node: '>=6'} + + imurmurhash@0.1.4: + resolution: {integrity: sha512-JmXMZ6wuvDmLiHEml9ykzqO6lwFbof0GG4IkcGaENdCRDDmMVnny7s5HsIgHCbaq0w2MyPhDqkhTUgS2LU2PHA==} + engines: {node: '>=0.8.19'} + is-binary-path@2.1.0: resolution: {integrity: sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==} engines: {node: '>=8'} @@ -213,10 +398,29 @@ packages: resolution: {integrity: sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==} hasBin: true + json-buffer@3.0.1: + resolution: {integrity: sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==} + + json-schema-traverse@0.4.1: + resolution: {integrity: sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==} + + json-stable-stringify-without-jsonify@1.0.1: + resolution: {integrity: sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==} + + keyv@4.5.4: + resolution: {integrity: sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==} + + levn@0.4.1: + resolution: {integrity: sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==} + engines: {node: '>= 0.8.0'} + locate-path@6.0.0: resolution: {integrity: sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==} engines: {node: '>=10'} + lodash.merge@4.6.2: + resolution: {integrity: sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==} + log-symbols@4.1.0: resolution: {integrity: sha512-8XPvpAA8uyhfteu8pIvQxpJZ7SYYdpUivZpGy6sFsBuKRY/7rQGavedeB8aK+Zkyq6upMFVL/9AW6vOYzfRyLg==} engines: {node: '>=10'} @@ -224,6 +428,9 @@ packages: lru-cache@10.4.3: resolution: {integrity: sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==} + minimatch@3.1.2: + resolution: {integrity: sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==} + minimatch@5.1.6: resolution: {integrity: sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==} engines: {node: '>=10'} @@ -244,10 +451,17 @@ packages: ms@2.1.3: resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} + natural-compare@1.4.0: + resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==} + normalize-path@3.0.0: resolution: {integrity: sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==} engines: {node: '>=0.10.0'} + optionator@0.9.4: + resolution: {integrity: sha512-6IpQ7mKUxRcZNLIObR0hz7lxsapSSIYNZJwXPGeF0mTVqGKFIXj1DQcMoT22S3ROcLyY/rz0PWaWZ9ayWmad9g==} + engines: {node: '>= 0.8.0'} + p-limit@3.1.0: resolution: {integrity: sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==} engines: {node: '>=10'} @@ -259,6 +473,10 @@ packages: package-json-from-dist@1.0.1: resolution: {integrity: sha512-UEZIS3/by4OC8vL3P2dTXRETpebLI2NiI5vIrjaD/5UtrkFX/tNbwjTSRAGC/+7CAo2pIcBaRgWmcBBHcsaCIw==} + parent-module@1.0.1: + resolution: {integrity: sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==} + engines: {node: '>=6'} + path-exists@4.0.0: resolution: {integrity: sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==} engines: {node: '>=8'} @@ -275,6 +493,14 @@ packages: resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==} engines: {node: '>=8.6'} + prelude-ls@1.2.1: + resolution: {integrity: sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==} + engines: {node: '>= 0.8.0'} + + punycode@2.3.1: + resolution: {integrity: sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==} + engines: {node: '>=6'} + randombytes@2.1.0: resolution: {integrity: sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==} @@ -286,6 +512,10 @@ packages: resolution: {integrity: sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==} engines: {node: '>=0.10.0'} + resolve-from@4.0.0: + resolution: {integrity: sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==} + engines: {node: '>=4'} + safe-buffer@5.2.1: resolution: {integrity: sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==} @@ -336,11 +566,22 @@ packages: resolution: {integrity: sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==} engines: {node: '>=8.0'} + type-check@0.4.0: + resolution: {integrity: sha512-XleUoc9uwGXqjWwXaUTZAmzMcFZ5858QA2vvx1Ur5xIcixXIP+8LnFDgRplU30us6teqdlskFfu+ae4K79Ooew==} + engines: {node: '>= 0.8.0'} + + uri-js@4.4.1: + resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==} + which@2.0.2: resolution: {integrity: sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==} engines: {node: '>= 8'} hasBin: true + word-wrap@1.2.5: + resolution: {integrity: sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==} + engines: {node: '>=0.10.0'} + workerpool@6.5.1: resolution: {integrity: sha512-Fs4dNYcsdpYSAfVxhnl1L5zTksjvOJxtC5hzMNl+1t9B8hTJTdKDyZ5ju7ztgPy+ft9tBFXoOlDNiOT9WUXZlA==} @@ -374,6 +615,63 @@ packages: snapshots: + '@eslint-community/eslint-utils@4.6.1(eslint@9.25.1)': + dependencies: + eslint: 9.25.1 + eslint-visitor-keys: 3.4.3 + + '@eslint-community/regexpp@4.12.1': {} + + '@eslint/config-array@0.20.0': + dependencies: + '@eslint/object-schema': 2.1.6 + debug: 4.4.0(supports-color@8.1.1) + minimatch: 3.1.2 + transitivePeerDependencies: + - supports-color + + '@eslint/config-helpers@0.2.1': {} + + '@eslint/core@0.13.0': + dependencies: + '@types/json-schema': 7.0.15 + + '@eslint/eslintrc@3.3.1': + dependencies: + ajv: 6.12.6 + debug: 4.4.0(supports-color@8.1.1) + espree: 10.3.0 + globals: 14.0.0 + ignore: 5.3.2 + import-fresh: 3.3.1 + js-yaml: 4.1.0 + minimatch: 3.1.2 + strip-json-comments: 3.1.1 + transitivePeerDependencies: + - supports-color + + '@eslint/js@9.25.1': {} + + '@eslint/object-schema@2.1.6': {} + + '@eslint/plugin-kit@0.2.8': + dependencies: + '@eslint/core': 0.13.0 + levn: 0.4.1 + + '@humanfs/core@0.19.1': {} + + '@humanfs/node@0.16.6': + dependencies: + '@humanfs/core': 0.19.1 + '@humanwhocodes/retry': 0.3.1 + + '@humanwhocodes/module-importer@1.0.1': {} + + '@humanwhocodes/retry@0.3.1': {} + + '@humanwhocodes/retry@0.4.2': {} + '@isaacs/cliui@8.0.2': dependencies: string-width: 5.1.2 @@ -386,6 +684,23 @@ snapshots: '@pkgjs/parseargs@0.11.0': optional: true + '@types/estree@1.0.7': {} + + '@types/json-schema@7.0.15': {} + + acorn-jsx@5.3.2(acorn@8.14.1): + dependencies: + acorn: 8.14.1 + + acorn@8.14.1: {} + + ajv@6.12.6: + dependencies: + fast-deep-equal: 3.1.3 + fast-json-stable-stringify: 2.1.0 + json-schema-traverse: 0.4.1 + uri-js: 4.4.1 + ansi-colors@4.1.3: {} ansi-regex@5.0.1: {} @@ -411,6 +726,11 @@ snapshots: binary-extensions@2.3.0: {} + brace-expansion@1.1.11: + dependencies: + balanced-match: 1.0.2 + concat-map: 0.0.1 + brace-expansion@2.0.1: dependencies: balanced-match: 1.0.2 @@ -421,6 +741,8 @@ snapshots: browser-stdout@1.3.1: {} + callsites@3.1.0: {} + camelcase@6.3.0: {} chalk@4.1.2: @@ -452,6 +774,8 @@ snapshots: color-name@1.1.4: {} + concat-map@0.0.1: {} + cross-spawn@7.0.6: dependencies: path-key: 3.1.1 @@ -466,6 +790,8 @@ snapshots: decamelize@4.0.0: {} + deep-is@0.1.4: {} + diff@5.2.0: {} eastasianwidth@0.2.0: {} @@ -478,6 +804,83 @@ snapshots: escape-string-regexp@4.0.0: {} + eslint-scope@8.3.0: + dependencies: + esrecurse: 4.3.0 + estraverse: 5.3.0 + + eslint-visitor-keys@3.4.3: {} + + eslint-visitor-keys@4.2.0: {} + + eslint@9.25.1: + dependencies: + '@eslint-community/eslint-utils': 4.6.1(eslint@9.25.1) + '@eslint-community/regexpp': 4.12.1 + '@eslint/config-array': 0.20.0 + '@eslint/config-helpers': 0.2.1 + '@eslint/core': 0.13.0 + '@eslint/eslintrc': 3.3.1 + '@eslint/js': 9.25.1 + '@eslint/plugin-kit': 0.2.8 + '@humanfs/node': 0.16.6 + '@humanwhocodes/module-importer': 1.0.1 + '@humanwhocodes/retry': 0.4.2 + '@types/estree': 1.0.7 + '@types/json-schema': 7.0.15 + ajv: 6.12.6 + chalk: 4.1.2 + cross-spawn: 7.0.6 + debug: 4.4.0(supports-color@8.1.1) + escape-string-regexp: 4.0.0 + eslint-scope: 8.3.0 + eslint-visitor-keys: 4.2.0 + espree: 10.3.0 + esquery: 1.6.0 + esutils: 2.0.3 + fast-deep-equal: 3.1.3 + file-entry-cache: 8.0.0 + find-up: 5.0.0 + glob-parent: 6.0.2 + ignore: 5.3.2 + imurmurhash: 0.1.4 + is-glob: 4.0.3 + json-stable-stringify-without-jsonify: 1.0.1 + lodash.merge: 4.6.2 + minimatch: 3.1.2 + natural-compare: 1.4.0 + optionator: 0.9.4 + transitivePeerDependencies: + - supports-color + + espree@10.3.0: + dependencies: + acorn: 8.14.1 + acorn-jsx: 5.3.2(acorn@8.14.1) + eslint-visitor-keys: 4.2.0 + + esquery@1.6.0: + dependencies: + estraverse: 5.3.0 + + esrecurse@4.3.0: + dependencies: + estraverse: 5.3.0 + + estraverse@5.3.0: {} + + esutils@2.0.3: {} + + fast-deep-equal@3.1.3: {} + + fast-json-stable-stringify@2.1.0: {} + + fast-levenshtein@2.0.6: {} + + file-entry-cache@8.0.0: + dependencies: + flat-cache: 4.0.1 + fill-range@7.1.1: dependencies: to-regex-range: 5.0.1 @@ -487,8 +890,15 @@ snapshots: locate-path: 6.0.0 path-exists: 4.0.0 + flat-cache@4.0.1: + dependencies: + flatted: 3.3.3 + keyv: 4.5.4 + flat@5.0.2: {} + flatted@3.3.3: {} + foreground-child@3.3.1: dependencies: cross-spawn: 7.0.6 @@ -503,6 +913,10 @@ snapshots: dependencies: is-glob: 4.0.3 + glob-parent@6.0.2: + dependencies: + is-glob: 4.0.3 + glob@10.4.5: dependencies: foreground-child: 3.3.1 @@ -512,10 +926,23 @@ snapshots: package-json-from-dist: 1.0.1 path-scurry: 1.11.1 + globals@14.0.0: {} + + globals@16.0.0: {} + has-flag@4.0.0: {} he@1.2.0: {} + ignore@5.3.2: {} + + import-fresh@3.3.1: + dependencies: + parent-module: 1.0.1 + resolve-from: 4.0.0 + + imurmurhash@0.1.4: {} + is-binary-path@2.1.0: dependencies: binary-extensions: 2.3.0 @@ -546,10 +973,27 @@ snapshots: dependencies: argparse: 2.0.1 + json-buffer@3.0.1: {} + + json-schema-traverse@0.4.1: {} + + json-stable-stringify-without-jsonify@1.0.1: {} + + keyv@4.5.4: + dependencies: + json-buffer: 3.0.1 + + levn@0.4.1: + dependencies: + prelude-ls: 1.2.1 + type-check: 0.4.0 + locate-path@6.0.0: dependencies: p-locate: 5.0.0 + lodash.merge@4.6.2: {} + log-symbols@4.1.0: dependencies: chalk: 4.1.2 @@ -557,6 +1001,10 @@ snapshots: lru-cache@10.4.3: {} + minimatch@3.1.2: + dependencies: + brace-expansion: 1.1.11 + minimatch@5.1.6: dependencies: brace-expansion: 2.0.1 @@ -592,8 +1040,19 @@ snapshots: ms@2.1.3: {} + natural-compare@1.4.0: {} + normalize-path@3.0.0: {} + optionator@0.9.4: + dependencies: + deep-is: 0.1.4 + fast-levenshtein: 2.0.6 + levn: 0.4.1 + prelude-ls: 1.2.1 + type-check: 0.4.0 + word-wrap: 1.2.5 + p-limit@3.1.0: dependencies: yocto-queue: 0.1.0 @@ -604,6 +1063,10 @@ snapshots: package-json-from-dist@1.0.1: {} + parent-module@1.0.1: + dependencies: + callsites: 3.1.0 + path-exists@4.0.0: {} path-key@3.1.1: {} @@ -615,6 +1078,10 @@ snapshots: picomatch@2.3.1: {} + prelude-ls@1.2.1: {} + + punycode@2.3.1: {} + randombytes@2.1.0: dependencies: safe-buffer: 5.2.1 @@ -625,6 +1092,8 @@ snapshots: require-directory@2.1.1: {} + resolve-from@4.0.0: {} + safe-buffer@5.2.1: {} serialize-javascript@6.0.2: @@ -673,10 +1142,20 @@ snapshots: dependencies: is-number: 7.0.0 + type-check@0.4.0: + dependencies: + prelude-ls: 1.2.1 + + uri-js@4.4.1: + dependencies: + punycode: 2.3.1 + which@2.0.2: dependencies: isexe: 2.0.0 + word-wrap@1.2.5: {} + workerpool@6.5.1: {} wrap-ansi@7.0.0: diff --git a/src/boolean/type.js b/src/boolean/type.js index 63b2ae7..b9f3287 100644 --- a/src/boolean/type.js +++ b/src/boolean/type.js @@ -1,6 +1,6 @@ import {BooleanT} from './BooleanT.js' import {match} from '#core/TypePatterns.js' -import {Returns, Type, TypeOfTypes, Undefined} from '#core/Type.js' +import {Returns, TypeOfTypes, Undefined} from '#core/Type.js' import {NumberT} from '#number/NumberT.js' const bool = f => Returns(BooleanT, f) diff --git a/src/complex/Complex.js b/src/complex/Complex.js index 9dc0630..2d308d6 100644 --- a/src/complex/Complex.js +++ b/src/complex/Complex.js @@ -11,7 +11,7 @@ function complexSpecialize(ComponentType) { const typeName = `Complex(${ComponentType})` if (ComponentType.concrete) { const fromSpec = [match( - ComponentType, math => r => ({re: r, im: ComponentType.zero}))] + ComponentType, () => r => ({re: r, im: ComponentType.zero}))] for (const {pattern, does} of ComponentType.from) { fromSpec.push(match( this.specialize(pattern.type), diff --git a/src/core/Type.js b/src/core/Type.js index 555b547..8fd5cb6 100644 --- a/src/core/Type.js +++ b/src/core/Type.js @@ -66,7 +66,7 @@ export const Undefined = new Type( t => typeof t === 'undefined', {zero: undefined, one: undefined, nan: undefined}) export const TypeOfTypes = new Type(t => t instanceof Type) -export const NotAType = new Type(t => true) // Danger, do not merge! +export const NotAType = new Type(() => true) // Danger, do not merge! NotAType._doNotMerge = true export const Returns = (type, f) => (f.returns = type, f) @@ -84,3 +84,17 @@ export const whichType = typs => Returns(TypeOfTypes, item => { } throw new TypeError(errorMsg) }) + +// The return typing strategies +// MAKE SURE NONE ARE FALSY, so that code can easily test whether a strategy +// has been specified. +export const ReturnTyping = Object.freeze({ + free: 1, + conservative: 2, + full: 3, + name(strat) { + for (const key in this) { + if (this[key] === strat) return key + } + } +}) diff --git a/src/core/TypeDispatcher.js b/src/core/TypeDispatcher.js index 2b997e6..b4da539 100644 --- a/src/core/TypeDispatcher.js +++ b/src/core/TypeDispatcher.js @@ -3,7 +3,7 @@ import ArrayKeyedMap from 'array-keyed-map' import {ResolutionError, isPlainFunction, isPlainObject} from './helpers.js' import {Implementations, ImplementationsGenerator} from './Implementations.js' import {bootstrapTypes} from './type.js' -import {Returns, whichType, Type} from './Type.js' +import {Returns, ReturnTyping, whichType, Type} from './Type.js' import { matched, needsCollection, Passthru, Matcher, match } from './TypePatterns.js' @@ -167,14 +167,18 @@ export class TypeDispatcher { if (typeof tryValue === 'object') { if (!('resolve' in tryValue)) { - tryValue.resolve = types => this.resolve(key, types) + tryValue.resolve = + (types, strat) => this.resolve(key, types, strat) } const get = () => { const keyDeps = this._dependencies.get(key) if (!keyDeps) return tryValue const watch = Array.from(keyDeps.keys().filter( - types => types.length === 0 - || this.resolve(key, types) === tryValue + bhvix => { + if (bhvix.length < 2) return true + const types = bhvix.slice(1) + return this.resolve(key, types) === tryValue + } )) if (watch.length) { return DependencyWatcher(tryValue, key, watch, this) @@ -221,7 +225,7 @@ export class TypeDispatcher { } } - _addToDeps(key, types) { + _addToDeps(key, bhvix) { // Never depend on internal methods: if (key.startsWith('_')) return let depMap = this._dependencies.get(key) @@ -229,11 +233,11 @@ export class TypeDispatcher { depMap = new ArrayKeyedMap() this._dependencies.set(key, depMap) } - if (!depMap.has(types)) depMap.set(types, new Map()) - const depColl = depMap.get(types) - for (const [dkey, types] of this.resolve._genDepsOf) { + if (!depMap.has(bhvix)) depMap.set(bhvix, new Map()) + const depColl = depMap.get(bhvix) + for (const [dkey, bhvix] of this.resolve._genDepsOf) { if (!depColl.has(dkey)) depColl.set(dkey, new ArrayKeyedMap()) - depColl.get(dkey).set(types, true) + depColl.get(dkey).set(bhvix, true) } } @@ -259,28 +263,42 @@ export class TypeDispatcher { return args => extractors.map(f => f(args)) } - resolve(key, types) { + // key is the identifier of the method being looked up + // types is a single argument type or an array of actual argument types + // strategy is a ReturnTyping value specifying how to choose the + // return type for the operation. + resolve(key, types, strategy) { if (!(key in this)) { throw new ReferenceError(`no method or value for key '${key}'`) } if (!Array.isArray(types)) types = [types] + if (!strategy) { + // Avoid recursing on obtaining config + if (key === 'config') strategy = ReturnTyping.free + else strategy = this.config.returnTyping + } + // The "behavior index": the return type strategy followed by the + // types: + const bhvix = [strategy, ...types] const generatingDeps = this.resolve._genDepsOf?.length - if (generatingDeps) this._addToDeps(key, types) + if (generatingDeps) this._addToDeps(key, bhvix) const behave = this._behaviors[key] // Return the cached resolution if it's there - if (behave.has(types)) { - const result = behave.get(types) + if (behave.has(bhvix)) { + const result = behave.get(bhvix) if (result === underResolution) { throw new ResolutionError( - `recursive resolution of ${key} on ${types}`) + `recursive resolution of ${key} on ${types} with return typing ` + + ReturnTyping.name(strategy) + ) } if (generatingDeps && typeof result === 'object' && !(result instanceof Type) ) { - return DependencyRecorder(result, key, this, types) + return DependencyRecorder(result, key, this, bhvix) } return result } @@ -315,12 +333,12 @@ export class TypeDispatcher { } // If this key is producing a non-function value, we're done if (!isPlainFunction(item)) { - behave.set(types, item) + behave.set(bhvix, item) if (generatingDeps && typeof item === 'object' && !(item instanceof Type) ) { - return DependencyRecorder(item, key, this, types) + return DependencyRecorder(item, key, this, bhvix) } return item } @@ -334,8 +352,8 @@ export class TypeDispatcher { let theBehavior = () => undefined let finalBehavior - this.resolve._genDepsOf.push([key, types]) - behave.set(types, underResolution) + this.resolve._genDepsOf.push([key, bhvix]) + behave.set(bhvix, underResolution) try { // Used to make sure not to return without popping _genDepsOf if (!('returns' in item)) { // looks like a factory @@ -345,6 +363,7 @@ export class TypeDispatcher { matched(template, this)) } catch (e) { e.message = `Error in factory for ${key} on ${types} ` + + `with return typing ${ReturnTyping.name(strategy)} ` + `(match data ${template}): ${e.message}` throw e } @@ -355,7 +374,8 @@ export class TypeDispatcher { const returning = theBehavior.returns if (!returning) { throw new TypeError( - `No return type specified for ${key} on ${types}`) + `No return type specified for ${key} on ${types} with` + + ` return typing ${ReturnTyping.name(strategy)}`) } if (needsCollection(template)) { // have to wrap the behavior to collect the actual arguments @@ -369,7 +389,7 @@ export class TypeDispatcher { } finally { this.resolve._genDepsOf.pop() // OK, now it's safe to return } - behave.set(types, finalBehavior) + behave.set(bhvix, finalBehavior) finalBehavior.template = template return finalBehavior } @@ -380,8 +400,8 @@ export class TypeDispatcher { _invalidate(depColl) { if (!depColl) return for (const [key, typeMap] of depColl) { - for (const types of typeMap.keys()) { - this._behaviors[key].delete(types) + for (const bhvix of typeMap.keys()) { + this._behaviors[key].delete(bhvix) } } } @@ -389,25 +409,26 @@ export class TypeDispatcher { _disengageFallback(key) { // We need to find all of the behaviors that currently rely on the // fallback, invalidate their dependencies, and remove them. - const fallTypes = [] + const fallIxes = [] const behs = this._behaviors[key] const imps = this._implementations[key] const deps = this._dependencies.get(key) - for (const types of behs.keys()) { + for (const bhvix of behs.keys()) { let fallsback = true for (const [pattern] of imps) { + const types = bhvix.length ? bhvix.slice(1) : bhvix const [finalIndex] = pattern.match(types) if (finalIndex === types.length) { fallsback = false break } } - if (fallsback) fallTypes.push(types) + if (fallsback) fallIxes.push(bhvix) } - for (const types of fallTypes) { - const depColl = deps?.get(types) + for (const bhvix of fallIxes) { + const depColl = deps?.get(bhvix) if (depColl?.size) this._invalidate(depColl) - behs.delete(types) + behs.delete(bhvix) } } @@ -415,20 +436,21 @@ export class TypeDispatcher { // like disengageFallback, just we have the offending pattern: const behs = this._behaviors[key] const deps = this._dependencies.get(key) - const patTypes = behs.keys().filter(types => { + const patIxes = behs.keys().filter(bhvix => { + const types = bhvix.length ? bhvix.slice(1) : bhvix const [finalIndex] = pattern.match(types) return finalIndex === types.length }) - for (const types of patTypes) { - const depColl = deps?.get(types) + for (const bhvix of patIxes) { + const depColl = deps?.get(bhvix) if (depColl?.size) this._invalidate(depColl) - behs.delete(types) + behs.delete(bhvix) } } } // Proxy that traps accesses and records dependencies on them -const DependencyRecorder = (object, path, repo, types) => new Proxy(object, { +const DependencyRecorder = (object, path, repo, bhvix) => new Proxy(object, { get(target, prop, receiver) { const result = Reflect.get(target, prop, receiver) // pass internal methods through, as well as resolve calls, @@ -443,27 +465,27 @@ const DependencyRecorder = (object, path, repo, types) => new Proxy(object, { // OK, it's not a method on a TypeDispatcher, it's some other kind of // value. So first record the dependency on prop at this path. const newPath = path ? [path, prop].join('.') : prop - repo._addToDeps(newPath, types) + repo._addToDeps(newPath, bhvix) // Now, if the result is an object, we may need to record further // dependencies on its properties (e.g. math.config.predictable) // So proxy the return value, except for types, which must maintain // strict referential identity: if (typeof result === 'object' && !(result instanceof Type)) { - return DependencyRecorder(result, newPath, repo, types) + return DependencyRecorder(result, newPath, repo, bhvix) } else return result } }) // The flip side: proxy that traps setting properties and invalidates things // that depend on them: -const DependencyWatcher = (object, path, typesList, repo) => new Proxy(object, { +const DependencyWatcher = (object, path, ixList, repo) => new Proxy(object, { set(target, prop, value, receiver) { // First see if this setting has any dependencies: const newPath = [path, prop].join('.') const depPerTypes = repo._dependencies.get(newPath) if (depPerTypes && Reflect.get(target, prop, receiver) !== value) { - for (const types of typesList) { - repo._invalidate(depPerTypes.get(types)) + for (const bhvix of ixList) { + repo._invalidate(depPerTypes.get(bhvix)) } } // Now we can just perform the setting @@ -474,7 +496,7 @@ const DependencyWatcher = (object, path, typesList, repo) => new Proxy(object, { const result = Reflect.get(target, prop, receiver) if (typeof result === 'object' && !(result instanceof Type)) { const newPath = [path, prop].join('.') - return DependencyWatcher(result, newPath, typesList, repo) + return DependencyWatcher(result, newPath, ixList, repo) } return result } diff --git a/src/core/TypePatterns.js b/src/core/TypePatterns.js index 14f41c0..0705894 100644 --- a/src/core/TypePatterns.js +++ b/src/core/TypePatterns.js @@ -2,7 +2,7 @@ import {Type, Undefined} from './Type.js' import {isPlainFunction} from './helpers.js' export class TypePattern { - match(typeSequence, options={}) { + match(_typeSequence, _options={}) { throw new Error('Specific TypePatterns must implement match') } sampleTypes() { diff --git a/src/core/__test__/Type.spec.js b/src/core/__test__/Type.spec.js index 84efb23..4570333 100644 --- a/src/core/__test__/Type.spec.js +++ b/src/core/__test__/Type.spec.js @@ -2,7 +2,7 @@ import assert from 'assert' import math from '#nanomath' import {NumberT} from '#number/NumberT.js' -import {Returns} from '../Type.js' +import {Returns, ReturnTyping} from '../Type.js' import {isPlainFunction} from '../helpers.js' describe('Core types', () => { @@ -40,4 +40,7 @@ describe('Core types', () => { assert(isPlainFunction(labeledF)) }) + it('provides return typing strategies', () => { + assert.strictEqual(ReturnTyping.name(ReturnTyping.full), 'full') + }) }) diff --git a/src/core/__test__/TypeDispatcher.spec.js b/src/core/__test__/TypeDispatcher.spec.js index c843e59..e8e1cf6 100644 --- a/src/core/__test__/TypeDispatcher.spec.js +++ b/src/core/__test__/TypeDispatcher.spec.js @@ -6,7 +6,7 @@ import * as numbers from '#number/all.js' import {NumberT} from '#number/NumberT.js' import {ResolutionError} from "#core/helpers.js" import {match, Any} from "#core/TypePatterns.js" -import {Returns, NotAType} from "#core/Type.js" +import {NotAType, Returns, ReturnTyping} from "#core/Type.js" import {plain} from "#number/helpers.js" describe('TypeDispatcher', () => { @@ -57,12 +57,15 @@ describe('TypeDispatcher', () => { it('detects dependencies on conversion operations', () => { const bgn = new TypeDispatcher(booleans, generics, numbers) const {BooleanT, NumberT} = bgn.types - assert(!bgn._behaviors.negate.has([BooleanT])) + assert(!bgn._behaviors.negate.has([ReturnTyping.free, BooleanT])) assert.strictEqual(bgn.negate(true), -1) - assert(bgn._behaviors.negate.has([BooleanT])) - const deps = bgn._dependencies.negate + assert(bgn._behaviors.negate.has([ReturnTyping.free, BooleanT])) + const deps = bgn._dependencies + .get('number') + .get([ReturnTyping.free, BooleanT]) + assert(deps.has('negate')) bgn.merge({number: match([BooleanT], Returns(NumberT, b => b ? 2 : 0))}) - assert(!bgn._behaviors.negate.has([BooleanT])) + assert(!bgn._behaviors.negate.has([ReturnTyping.free, BooleanT])) assert.strictEqual(bgn.negate(true), -2) }) it('disallows merging NotAType', () => { diff --git a/src/core/config.js b/src/core/config.js new file mode 100644 index 0000000..3c26db5 --- /dev/null +++ b/src/core/config.js @@ -0,0 +1,11 @@ +import {ImplementationsGenerator} from './Implementations.js' +import {ReturnTyping} from './Type.js' +import {match, Passthru} from './TypePatterns.js' + +export const config = new ImplementationsGenerator(() => match(Passthru, { + // default comparison tolerances: + relTol: 1e-12, + absTol: 1e-15, + // Strategy for choosing operation return types: + returnTyping: ReturnTyping.free, +})) diff --git a/src/core/type.js b/src/core/type.js index 7452dd4..e5f4ecf 100644 --- a/src/core/type.js +++ b/src/core/type.js @@ -1,3 +1,4 @@ +import {config} from './config.js' import {ImplementationsGenerator} from './Implementations.js' import {Type, TypeOfTypes, Undefined, whichType} from './Type.js' import {match, Passthru} from './TypePatterns.js' @@ -22,5 +23,5 @@ export const types = new ImplementationsGenerator(() => match(Passthru, {})) // an explicitly ordered export of implementations for this sake: export const bootstrapTypes = { - types, Type, Undefined, TypeOfTypes, typeOf + types, config, Type, Undefined, TypeOfTypes, typeOf } diff --git a/src/generic/all.js b/src/generic/all.js index 4dea4d2..89d8802 100644 --- a/src/generic/all.js +++ b/src/generic/all.js @@ -1,4 +1,3 @@ export * as arithmetic from './arithmetic.js' -export * as configuration from './config.js' export * as relational from './relational.js' export * as utilities from './utils.js' diff --git a/src/generic/config.js b/src/generic/config.js deleted file mode 100644 index 72f8ced..0000000 --- a/src/generic/config.js +++ /dev/null @@ -1,5 +0,0 @@ -import {ImplementationsGenerator} from '#core/Implementations.js' -import {match, Passthru} from '#core/TypePatterns.js' - -export const config = new ImplementationsGenerator( - () => match(Passthru, {relTol: 1e-12, absTol: 1e-15})) diff --git a/src/number/relational.js b/src/number/relational.js index a4820a6..2d2e8df 100644 --- a/src/number/relational.js +++ b/src/number/relational.js @@ -1,4 +1,3 @@ -import {Returns} from '#core/Type.js' import {match, Optional} from '#core/TypePatterns.js' import {boolnum} from './helpers.js' import {NumberT} from './NumberT.js' diff --git a/src/number/utils.js b/src/number/utils.js index 01fdbe4..c8994c0 100644 --- a/src/number/utils.js +++ b/src/number/utils.js @@ -1,7 +1,6 @@ import {plain, boolnum} from './helpers.js' import {NumberT} from './NumberT.js' -import {Returns} from '#core/Type.js' import {match} from '#core/TypePatterns.js' export const clone = plain(a => a)