I read that CL supports optional typing (as in the language has this feature) which I would assume allos for faster code in many cases, but I can't seem to find anything about actually writing typed code. Is it possible to explicitly type code instead of using hacks? (for example, #'vector
generates a simple-vector
, kinda reminds me of |0
in JS to coerce to an integer)
Or maybe since types are actually CLOS classes (they are, right?) you simply have to #'make-instance
an object of type, say, 'integer
?
Common Lisp allows to define types and to declare types for variables, functions, ...
Types are independent from classes - classes are also types, but types can express more (like the range of integers, the size of an array, the element types of an array, ...).
Declaring types may serve a lot of different purposes:
Thus the effects of declaring types with certain compiler settings (debug, space, speed, compilation-speed, safety, ..) is not very simple. A Lisp system may ignore much of the declarations or use them widely. The effects can be very different. In some combination of compiler settings declaring a type may make the code a lot slower, in others it may make it a lot faster.
Also, compilers might be able to do some amount of type inference.
A simple declaration might look like this:
The Lisp system can now do one or more of the following:
a
andb
are integers and that the result of the + operation is actually an integer (and not a float, rational or complex number)+
operation+
operation, which only works on integersTypical compiler settings to achieve above would be:
But the exact settings and their meaning might differ and should be documented in the specific Lisp implementation manual.
The types are documented in the ANSI Common Lisp Standard. See Types and Classes.
For the compiler settings see for example: SBCL Compiler or LispWorks, The Compiler.
To study the effect of compilation of type declared code one can use disassemble and time.