3.3 Using type-specific operations
(defun add-list (l) (let ((sum 0)) (declare (fixnum sum)) (dolist (i l sum) (declare (fixnum i)) (setq sum (+ sum (the fixnum (if (> i 0) i (- i))))))))
sum
is propagated to the expression whose value is assigned tosum
. The explicitly declared type of the second term ofsum
is propagated to thei
and (-i
) components of theif
expression. The Compiler can thus compile all of the operators as fixnum-specific operators.
defstruct
.bird
and an instancebird1
as follows:
(defstruct bird (weight 0.0 :type float) (height 0.0 :type float) (age 0 :type fixnum))(setf bird1 (make-bird :age 1))
age
field has been declared to be of type fixnum
, in the following code the Compiler expects both the argument to1+
and the returned value of the expression to be fixnums:
(setf (bird-age bird1) (1+ (bird-age bird1)))
1+
.
ftype
orrestrictive-ftype
declarations (see Section 3.2.3 on page 36).inline
allows the Compiler to propagate type information within the body of that function. For example, the following code defines a function,use-square
, that callssquare
:(defun use-square (z) (declare (fixnum z)) (the fixnum (square z)))
z
and the result ofsquare
are both declared to be of typefixnum
, the type information propagates to the call to*
in the body ofsquare
. The Compiler can thus use a fixnum-specific form of the*
operator.
Generated with Harlequin WebMaker