3.2 Making declarations

3.2.3 FTYPE declarations

Anftype declaration specifies the manner in which the returned value type of a declared function depends on the argument types of the function. Whenever the arguments to a declared function are of the indicated types, the result of the function will also be of the indicated type. A function can have more than oneftype declaration associated with it. You would use anftype declaration when you want to conditionally restrict the result of a function every time the function is called.

Anftype declaration has the following form:

(declare (ftype type function-name-1 function-name-2 ...))
Afunction declaration is an abbreviated form of anftype declaration. It has the following form:

(declare (function name arglist result-type-1 result-type-2 ...))
Anftype declaration does not require the arguments to an expression to be of a particular type; it merely specifies that the result of the function will be of a certain type if the arguments of the function have been declared as a certain type. If the arguments are not of the specified type, no error is signaled. For example, the following proclamation declares that if the argument to the functionsquare is a fixnum integer, the value of the function will also be a fixnum integer:

(proclaim '(ftype (function (fixnum) fixnum) square))
(defun square (x) (* x x))
The proclamation has no effect on the following code because the argumentx is not declared to be of typefixnum:

(defun do-some-arithmetic (x)
  (the fixnum (+ x (square x))))

If, however, you add atype declaration forx, the Compiler can assume that the expression (square x) is a fixnum, and it will use the fixnum-specific version of the+ operator.

(defun do-some-arithmetic (x)
  (declare (type fixnum x))
  (the fixnum (+ x (square x))))


The Advanced User's Guide - 9 SEP 1996

Generated with Harlequin WebMaker