Compile-time type checking in Common Lisp: a small experiment

(Posted on 2022-05-09)

In static1.lisp:

(defpackage static1 
 (:use #:cl)) 
(in-package :static1) 

(declaim (ftype (function (string string) string) concat-strings)) 
(defun concat-strings (a b)
  (format nil "~A --- ~A" a b)) 

(declaim (ftype (function (fixnum fixnum) string) concat-nums)) 
(defun concat-nums (a b)
  (concat-strings a b))

Then I ran this command:

sbcl --noinform --eval '(compile-file "static1.lisp")' --quit

SBCL showed this warning:

; caught WARNING: 
;  Derived type of STATIC1::A is 
;    (VALUES FIXNUM &OPTIONAL), 
;  conflicting with its asserted type 
;    STRING. 
;  See also: 
;    The SBCL Manual, Node "Handling of Types"

So SBCL – a Common Lisp implementation, can check type in compile-time. Anyway, a programmer needs to read warnings.