source generics.lsp
Module: generics
Author: Jeff Ober
Version: 1.2
Location: http://static.artfulcode.net/newlisp/generics.lsp
Provides generic functions for newLISP. Requires the util module.
Version history
1.2 • methods may now use args
1.1 • fixed bug in method lookups that disallowed certain similar type combinations
1.0 • initial release- § -
define-method
syntax: (define-method (sym-name (string-type-1 (sym-param-1 default-value-1)) ...) body)
parameter: sym-name - the method's name
parameter: string-type-n - the type of the nth parameter
parameter: sym-param-n - the name of the nth parameter
parameter: default-value-n - optional; the default nth of the first parameter
parameter: body - the forms that make up the function body
Creates a generic function as sym-name that corresponds to the types of the parameters passed. Passed parameters are a list of a string type and a normal function parameter, which may be either a symbol or a list of a symbol and the parameter's default value.
Generic functions are parametrically polymorphic. This means that a function may have multiple type signatures, and the code block that is applied is determined at run-time by the types of the values passed to it.
Note that all listed parameters are required for generic functions. This additionally means that the practice of using "empty" parameters after a comma is not supported for generic functions either.
Each parameter may consist of:(string-type (sym-param default-value)) (string-type sym-param) (sym-param default-value) sym-param
Parameters without a type will match any type of value. Parameters with a type must match the passed value's type to match with a generic function.
example:(define-method (area ("integer" x) ("integer" y)) (* x y)) (area 3 4) => 12 (area 3 "four") => error! (define-method (area ("integer" x) ("string" y)) (format "%d times %s" x y)) (area 3 "four") => "3 times four"- ∂ -
Artful Code
generated with newLISP and newLISPdoc