TYPE(3cl) |
Common Lisp Reference |
TYPE(3cl) |
NAME
type – specify that values of variables will only be of specified types (declaration)
SYNOPSIS
(
type
) |
ARGUMENTS and VALUES
—a type specifier.
—a variable name.
VALID CONTEXT
declaration or proclamation
BINDING TYPES AFFECTED
variable
DESCRIPTION
Affects only variable bindings and specifies that the setq, as well as the initial values of the s must be of the specified . type declarations never apply to function bindings (see ftype). s take on values only of the specified . In particular, values assigned to the variables by
A type declaration of a symbol defined by symbol-macrolet is equivalent to wrapping a the expression around the expansion of that symbol, although the symbol’s macro expansion is not actually affected.
The meaning of a type declaration is equivalent to changing each reference to a variable ( ) within the scope of the declaration to (the ), changing each expression assigned to the variable ( ) within the scope of the declaration to (the ), and executing (the ) at the moment the scope of the declaration is entered.
A type declaration is valid in all declarations. The interpretation of a type declaration is as follows:
During the execution of any reference to the declared variable within the scope of the declaration, the consequences are undefined if the value of the declared variable is not of the declared type.
During the execution of any setq of the declared variable within the scope of the declaration, the consequences are undefined if the newly assigned value of the declared variable is not of the declared type.
At the moment the scope of the declaration is entered, the consequences are undefined if the value of the declared variable is not of the declared type.
A type declaration affects only variable references within its scope.
If nested type declarations refer to the same variable, then the value of the variable must be a member of the intersection of the declared types.
If there is a local type declaration for a dynamic variable, and there is also a global type proclamation for that same variable, then the value of the variable within the scope of the local declaration must be a member of the intersection of the two declared types.
type declarations can be free declarations or bound declarations.
A symbol cannot be both the name of a type and the name of a declaration. Defining a symbol as the name of a class, structure, condition, or type, when the symbol has been declared as a declaration name, or vice versa, signals an error.
Within the lexical scope of an array type declaration, all references to array elements are assumed to satisfy the expressed array element type (as opposed to the upgraded array element type). A compiler can treat the code within the scope of the array type declaration as if each access of an array element were surrounded by an appropriate the form.
AFFECTED BY
(none)
EXCEPTIONAL SITUATIONS
(none)
NOTES
( ) is an abbreviation for (type var).
A type declaration for the arguments to a function does not necessarily imply anything about the type of the result. The following function is not permitted to be compiled using implementation-dependent fixnum-only arithmetic:
(defun () (declare (fixnum )) (+ ))
To see why, consider ( most-positive-fixnum 1). Common Lisp defines that must return a bignum here, rather than signal an error or produce a mathematically incorrect result. If you have special knowledge such “fixnum overflow” cases will not come up, you can declare the result value to be in the fixnum range, enabling some compilers to use more efficient arithmetic:
(defun ()
(declare (fixnum ))
(the fixnum (+ )))
Note, however, that in the three-argument case, because of the possibility of an implicit intermediate value growing too large, the following will not cause implementation-dependent fixnum-only arithmetic to be used:
(defun ()
(declare (fixnum ))
(the fixnum (+ )))
To see why, consider ( most-positive-fixnum 1 -1). Although the arguments and the result are all fixnums, an intermediate value is not a fixnum. If it is important that implementation-dependent fixnum-only arithmetic be selected in implementations that provide it, consider writing something like this instead:
(defun ()
(declare (fixnum ))
(the fixnum (+ (the fixnum (+ )) )))
EXAMPLES
(
defun
(
)
(
declare
(
type fixnum
))
(
let
((
(
+
)))
(
declare
(
type fixnum
))
))
(
1 2
)
3
;; The previous definition of F is equivalent to
(
defun
(
)
;; This declaration is a shorthand form of the
;; TYPE declaration
(
declare
(
fixnum
))
;; To declare the type of a return value, it’s not
;; necessary to create a named variable. A THE special
;; form can be used instead.
(
the fixnum
(
+
)))
(
1 2
)
3
(
defvar
(
make-array
10
:element-type
’(
signed-byte
5
)))
(
defvar
(
make-array
10
:element-type
’(
signed-byte
8
)))
(
defun
(
)
(
declare
(
type
(
array
(
signed-byte
5
)
1
)
))
(
setf
(
aref
1
)
31
)
(
setf
(
aref
2
)
127
)
(
setf
(
aref
3
)
(
*
2
(
aref
3
)))
(
let
((
0
))
(
declare
(
type
(
signed-byte
5
)
))
(
setf
(
aref
0
))))
(
)
(
)
The above definition of is equivalent to:
(
defun
(
)
(
setf
(
the
(
signed-byte
5
) (
aref
1
))
31
)
(
setf
(
the
(
signed-byte
5
) (
aref
2
))
127
)
(
setf
(
the
(
signed-byte
5
) (
aref
3
))
(
*
2
(
the
(
signed-byte
5
) (
aref
3
))))
(
let
((
0
))
(
declare
(
type
(
signed-byte
5
)
))
(
setf
(
the
(
signed-byte
5
) (
aref
0
)))))
Given an implementation in which fixnums are 29 bits but fixnum arrays are upgraded to signed 32-bit arrays, the following could be compiled with all fixnum arithmetic:
(defun ()
(declare (type (array fixnum *) ))
(dotimes ( (length ))
(incf (aref ))))
SEE ALSO
declare(3cl), declaim(3cl), proclaim(3cl)
AUTHOR and COPYRIGHT
Substantial portions of this page are taken from draft proposed American National Standard for Information Systems—Programming Language—Common Lisp, X3J13/94-101R, Version 15.17R, Fri 12-Aug-1994 6:35pm EDT; no copyright indicated.
Additional clarification and comments by Michael Marking <marking@tatanka.com>, http://www.tatanka.com/software/cl-manpages/; alternatively, https://github.com/wakinyantanka/cl-manpages/. Copyright 2017 Michael Marking as both an original and a derivative work.
Licensed under Creative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0).
This page last revised Sunday 26 February 2017.