When I define a function in Common Lisp like this:
(defun foo (n)
(declare (type fixnum n))
(+ n 42))
I expected a call like (foo "a") to fail right away but it instead fail at the call to +. Is the declare form not guarantees static type checking?
Type declarations are traditionally meant to be used as guarantees to the compiler for optimization purposes. For type checking, use
check-type(but note that it, too, does the checking at run-time, not at compile-time):That said, different Common Lisp implementations interpret type declarations differently. SBCL, for example, will treat them as types to be checked if the
safetypolicy setting is high enough.In addition, if you want static checking, SBCL is probably your best bet as well, since its type inference engine warns you about any inconsistencies it encounters. To that end,
ftypedeclarations can be put to good use: