Types and Declarations

Optimize for runtime speed Runtime checks (0 = unchecked: dangerous) Debuggability (locals, backtraces)

SBCL treats declarations as assertions (under safety ≥ 1) and performs type inference. Compiling with (speed 3) makes SBCL print optimization notes explaining what it couldn't optimize.

Coalton (Module 54) adds Haskell-style static typing embedded in CL.

;; Type specifiers
'integer 'fixnum '(integer 0 255) '(unsigned-byte 8) 'single-float 'double-float 'string
'(simple-array double-float (*)) '(vector fixnum) '(or null string) '(member :a :b) '(satisfies evenp)

(typep 5 '(integer 0 10))                ; T
(subtypep 'fixnum 'integer)              ; T, T
(deftype octet () '(unsigned-byte 8))
(deftype small-int () '(integer 0 100))
(coerce 1/2 'double-float)               ; 0.5d0

;; Declarations
(defun add-fixnums (a b)
  (declare (type fixnum a b)             ; argument types
           (optimize (speed 3) (safety 0)))   ; local optimization settings
  (the fixnum (+ a b)))                  ; THE: assert the result type

(declaim (ftype (function (fixnum fixnum) fixnum) add-fixnums))   ; global function type
(declaim (inline square))                ; inline hint
(declaim (optimize (speed 1) (safety 1) (debug 1)))               ; global defaults

;; Typed slots, structs, and arrays
(defstruct vec3
  (x 0d0 :type double-float)
  (y 0d0 :type double-float)
  (z 0d0 :type double-float))