JS-CLOS
A CLOS-like object system in JavaScript.
- Multiple inheritance
- Multiple dispatch
- Type checking on construction
- ML (or Haskell) style datatype
- Guarded functions (simple patternmatching)
- Memoization (optional)
- Partial application
Examples use cases are shown in:
Usage
Guarded Functions and Memoization
var fib = ; //pass true to enable memoization;;;
Partial Application
p = ;var add = ;;; p; //=> [3,4,5,6,7]p; //=> ["lisp!!!", "alien!!!", "rocks!!!"]
Simple Data Class
//class, when `make`d, retruns a hash of valuesvar book = ; //generic function showvar show = ; //show an instance of book; var p_city = ; ;
Multimethod
//define a bunch of classes//the name is optionalvar floor = ;var carpet = ;var ball = ;var glass = ;var stick = ; //function to display the resultvar { console;}; //define a generic function `bump`var bump = ; //define methods;;; //if you prefer, the following works, toobump; //call the methods; //should bounce; //should crash; //shold silince ; // undefined method
API
define_class
Takes:
- an arrey of classes to inherit from. (required)
- a validator function called upon instance construction. (optional)
- a name string which is used as the string representation of its instances (optional)
Returns a constructor function (called class) that can be new
ed or get applied to make
Syntax
define_class([ parent classes ], function (x) { protocol }, name);
Example
var x = ;var y = ;var z = ;
define_generic
Takes nothing.
Returns a generic function.
Syntax
define_generic();
Example
var show = ;
define_method
Takes:
- a generic function (required)
- an array of patterns that specifies the type of arguments given to the method (required)
- a function that is the body of the method (required)
A pattern is either
- a class (compared with
instanceof
) - a value (compared with
===
) - a constructor (that is defined with
define_constructor
) - a string returned by
typeof
operator ("number", "function", etc) - undefined for wildcard
Returns void.
Syntax
define_method( generic function , [ patternA , ... ], function ( a , ... ) { body });
Example
; ;
define_constructor
define_constructor
allows a class to have multiple constructors.
Takes:
- a class (required)
- an initialization function (optional)
Returns a constructor function.
Syntax
define_constructor( class , function (...) { return make(class, {...}); });
Example
var rank = ;var Ace = ;var Jack = ;var Queen = ;var King = ;var Num = ; ; //true
make
Takes:
- a class (required)
- a hash object specifying the initial values of each slot (optional)
Returns an instance of the class.
Syntax
make( class, { slot_name: initial_value, ...});
Example
;;; //ERROR
Note
The hash object given as the second argument is matched with the function given as the second argument to define_class
. If the result is false, an exception gets thrown.
is_a
Takes:
- an instance (required)
- a class or a string (required)
Returns boolean
slot_exists
Takes:
- an instance (required)
- a slot identifier (required)
- a class or a string to specify the type (optional)
Returns a boolean. True if the instance has the slot of the specified type. False otherwise.
defClass, defGeneric, defMethod, isA
Aliaces.