generic-function

0.1.1 • Public • Published

Generic Functions for Node.js

1. Overview

Generic Functions for Node.js is an implementation of generic functions for Node.js, based on the Common Lisp Object System.

Here's the run-down. A generic function is a callable function that dispatches to different specializations ("methods") for different parameters. Primitive object-oriented systems use single dispatch dispatch only the first argument (usually the first one left to the dot) whereas advanced ones dispatch on all their arguments (which makes dot notation unnecessary).

2. Example

require("generic-function");

var collide = defgeneric(2);
defmethod(collide, "string", "number", function (x, y) {
	console.log("A %s collided with %s pieces of asteriods.", x, y);
});
defmethod(collide, "number", "string", function (x, y) {
	console.log("%s pieces of asteroids collided with a %s.", x, y);
});
collide("spaceship", 42);
"A spaceship collided with 42 pieces of asteriods."
collide(23, "space station");
"23 pieces of asteroids collided with a space station."

3. Supported Functionality

  • Core dispatch functionality (but without walking the prototype hierarchy yet)
  • Adding methods at any time
  • Removing methods at any time
  • Finding a method with a particular type signature

4. Open Questions

  • Should intrinsic types be dispatched like their object-based counterparts? I.e., should 'string' be dispatched as a 'String' and vice-versa?
  • Method dispatch examines leaf object types, but not the prototype hierarchy. Walk the prototype hierarchy to find the matching method.
  • Support 0-arg generic functions.

Readme

Keywords

none

Package Sidebar

Install

npm i generic-function

Weekly Downloads

4

Version

0.1.1

License

none

Last publish

Collaborators

  • ceineke