Skip to content

Commit

Permalink
feat(defmulti): add opt fallback arg for defmultiN(), update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 2, 2019
1 parent cd17586 commit 1d29153
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/defmulti/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 10 additions & 5 deletions packages/defmulti/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -244,10 +245,14 @@ const makeRels = (spec: AncestorDefs) => {
* ```
*
* @param impls
* @param fallback
*/
export function defmultiN<T>(impls: { [id: number]: Implementation<T> }) {
export function defmultiN<T>(
impls: { [id: number]: Implementation<T> },
fallback?: Implementation<T>
) {
const fn = defmulti<T>((...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]);
}
Expand Down

0 comments on commit 1d29153

Please sign in to comment.