diff --git a/packages/defmulti/README.md b/packages/defmulti/README.md index f8ad7219cb..0e6e55e5c0 100644 --- a/packages/defmulti/README.md +++ b/packages/defmulti/README.md @@ -110,9 +110,9 @@ foo.rels(); Returns a multi-dispatch function which delegates to one of the provided implementations, based on the arity (number of args) when the function is called. Internally uses `defmulti`, so new arities can be dynamically -added (or removed) at a later time. `defmultiN` also registers a -`DEFAULT` implementation which simply throws an `IllegalArityError` when -invoked. +added (or removed) at a later time. If no `fallback` is provided, +`defmultiN` also registers a `DEFAULT` implementation which simply +throws an `IllegalArityError` when invoked. **Note:** Unlike `defmulti` no argument type checking is supported, however you can specify the return type for the generated function. diff --git a/packages/defmulti/src/index.ts b/packages/defmulti/src/index.ts index f355416f95..202714b8f7 100644 --- a/packages/defmulti/src/index.ts +++ b/packages/defmulti/src/index.ts @@ -215,9 +215,10 @@ const makeRels = (spec: AncestorDefs) => { * Returns a multi-dispatch function which delegates to one of the * provided implementations, based on the arity (number of args) when * the function is called. Internally uses `defmulti`, so new arities - * can be dynamically added (or removed) at a later time. `defmultiN` - * also registers a `DEFAULT` implementation which simply throws an - * `IllegalArityError` when invoked. + * can be dynamically added (or removed) at a later time. If no + * `fallback` is provided, `defmultiN` also registers a `DEFAULT` + * implementation which simply throws an `IllegalArityError` when + * invoked. * * **Note:** Unlike `defmulti` no argument type checking is supported, * however you can specify the return type for the generated function. @@ -244,10 +245,14 @@ const makeRels = (spec: AncestorDefs) => { * ``` * * @param impls + * @param fallback */ -export function defmultiN(impls: { [id: number]: Implementation }) { +export function defmultiN( + impls: { [id: number]: Implementation }, + fallback?: Implementation +) { const fn = defmulti((...args: any[]) => args.length); - fn.add(DEFAULT, (...args) => illegalArity(args.length)); + fn.add(DEFAULT, fallback || ((...args) => illegalArity(args.length))); for (let id in impls) { fn.add(id, impls[id]); }