Programming Language for Old Timers


by David A. Moon
February 2006 .. September 2008

Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.


Previous page   Table of Contents   Next page


Method Heads

A method head is used in the defun and defprotocol definition statements and in the require statement to identify a method. It specifies a function name and a parameter list, generally with types declared for some or all of the parameters. The syntactic type method-head is defined in the compiler module for this purpose. It parses into a parameter-list object with a function name.

A method head can be written either in the form of a function call using a parenthesized argument list or in the form of a prefix or infix operator or macro call. Thus a method head can resemble any of the ways that a function invocation can be written.

An assignment right-hand-side parameter can be appended using :=. This modifies the function name.

A result type can be appended using is (not used with :=).

Note that when using a parenthesized argument list, if the function is defined as an operator or a macro its name must be denatured with \.

When an assignment is specified the right-hand-side parameter is appended to the parameter list as an additional required parameter (no optional or keyword parameters are allowed) and the name becomes the function name concatenated with :=. This is the normal function of the := operator.

There is a special rule for the case where an assignment is specified, the right-hand-side parameter's type is unspecified or is anything, and the parameter-list contains a rest parameter. In this case, the right-hand-side parameter is discarded. The method is assumed to use the last element of the value of the rest parameter as a new value to be assigned, and to use the remaining elements of the value of the rest parameter as part of the specification of where to make the assignment. This is the only case where an assignment can be specified and the parameter list can contain other than required parameters.

There is no separate syntax for method-head. Instead the method-head parser parses any expression and disassembles the resulting P-expression as follows:

A parameter list can only contain one rest parameter. As a special case, if there is one more argument expression after the rest parameter, that argument is simply a name, and the function name ends in ":=", the special rule for assignment applies and the extra argument is ignored.

Examples of method heads:

f(x)
f(x is integer)
f(x) is integer
f(x is integer, optional: y is integer = 0)
f(x, y, optional: z = 100) is list or false
f(x, rest: y)
f(x, optional: y, key: a, b, c)
f(x, y) := z
f(x is integer, y is list) := z is sequence

-(x is integer) is integer

(x is integer) + (y is integer) is integer

(x is dictionary)[key, key: default]

\is(object, type is class) is boolean


Previous page   Table of Contents   Next page