Previous page Table of Contents Next page
A method requirement specification (defprotocol or require), a method definition (defun), a class constructor specification (in defclass), and a function constructor (fun) each use some form of parameter list to describe the arguments received by a function and the value returned.
There are four kinds of parameter that can be bound to arguments passed to a function: required, optional, keyword, and rest. Unless otherwise specified, parameters are required. The keyword optional: indicates that the following parameters are optional. The keyword key: indicates that all remaining parameters are optional and matched by keyword rather than by position. The keyword rest: indicates that exactly one parameter follows and it receives a keyed-sequence of all remaining arguments. Optional, keyword, and rest parameters work much the same way as in Common Lisp, except a parameter list cannot have both rest and keyword parameters.
The name of a parameter can be simply a name, or can be an invocation of a constructor function that has an associated destructuring macro. In the simple case, a variable with the specified name is bound to the argument. When destructuring is used, the argument received by the parameter is taken apart and one or more variables are bound to its parts. Destructuring can only be used with required and optional parameters. The details of destructuring are explained in the Destructuring section below.
Each variable bound by a parameter list has a fixed definition visible in the body of a method or the initialization expressions of a class. The definition of a parameter variable is never assignable.
Each parameter has an optional type restriction, using the is operator. Type restrictions control the selection phase of function invocation. The value of an argument that matches this parameter must be of the specified type. A type restriction on a rest parameter applies to each individual argument that becomes an element of the rest parameter. If no type restriction is specified, the default type is anything unless destructuring is used.
Each optional or keyword parameter has an optional default value expression, specified with the = operator, which is evaluated and used when there is no corresponding argument in a function call. A default value expression is evaluated in a scope in which the preceding parameter variables are visible. If no default value expression is specified, the default default value is false.
The result type of a method requirement specification, a method definition, or a function constructor is specified using the is operator. The method's result is restricted to the specified type. If no result type is specified, the default type is anything.
The syntax for a parameter list depends on context. In a method requirement specification or a method definition the most general form is used, called a method head. This has the syntactic form of a function call, called a "prototype call." Each argument expression in the function call is reinterpreted as a parameter specification. The prototype call can be the left-hand argument to an is operator that specifies the result type. See the next section for details.
In a function constructor the parameter list has the syntactic form of a function call using the ( operator, but the function name is optional and usually omitted. Again the is operator can be used to specify the result type.
A class constructor specification is similar to a function constructor but there is no result type so the is operator cannot be used.
Parsers for input-parameter-list, parameter-list, and method-head are defined in the compiler module. They all return instances of the class parameter-list. parameter-list has slots for each kind of parameter, the result-type, the function name (only used in method heads), and the prologue code (used by destructuring). The parameter-list parser parses the parenthesized form used by function constructors which allows a result type to be specified with is after the right parenthesis. The input-parameter-list parser parses the parenthesized form used by class constructor specifications which doesn't allow result types and returns a parameter-list . The method-head parser parses the general prototype call form which specifies function name, parameters, and result.
defclass parameter-list required-parameters is typed-variable optional-parameters is typed-variable or defaulted-typed-variable keyword-parameters is typed-variable or defaulted-typed-variable rest-parameter is typed-variable or false result-type is expression or false function-name is name or false prologue is expression or false defclass typed-variable name is name type is expression defclass defaulted-typed-variable is typed-variable default-expression is expression
Actually, the union type typed-variable or defaulted-typed-variable is the same type as typed-variable. The more complex expression is only used to emphasize that a defaulted-typed-variable can be used and its default-expression will be effective.
Previous page Table of Contents Next page