DEV Community

Vee Satayamas
Vee Satayamas

Posted on

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

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))
Enter fullscreen mode Exit fullscreen mode

Then I ran this command:

sbcl --noinform --eval '(compile-file "static1.lisp")' --quit
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)