I am trying to understand why when I declare types, things seem to go very wrong in SBCL.
Here's a little file I created to reproduce the problem:
(defpackage p1 (:use :cl) (:export #:do-it #:keytype))
(in-package :p1)
(deftype keytype ()
`(member :white :black nil))
(declaim (ftype (function (&key (foo keytype))) do-it))
(defun do-it (&key foo)
(format T "key foo: ~A~%" foo))
(defpackage p2 (:use :cl :p1))
(in-package :p2)
(do-it :foo :black)
(loop for i from 0 to 2
do (loop for j from 0 to 3
for flag = (evenp i) then (not flag)
do (do-it :foo (if flag :white :black))))
When I load this for the first time in SLIME, I get this:
; SLIME
; file: /Users/renatoathaydes/programming/projects/format-ansi/tests/checking.lisp
; in: DEFUN DO-IT
; (DEFUN P1:DO-IT (&KEY P1::FOO) (FORMAT T "key foo: ~A~%" P1::FOO))
;
; caught STYLE-WARNING:
; Defining a :FOO keyword not present in previous declaration.
;
; caught STYLE-WARNING:
; The definition lacks the FOO key present in previous declaration.
;
; compilation unit finished
; caught 2 STYLE-WARNING conditions
key foo: BLACK
; file: /Users/renatoathaydes/programming/projects/format-ansi/tests/checking.lisp
; in: LOOP FOR
; (P1:DO-IT :FOO
; (IF P2::FLAG
; :WHITE
; :BLACK))
;
; caught WARNING:
; :FOO is not a known argument keyword.
;
; compilation unit finished
; caught 1 WARNING condition
key foo: WHITE
key foo: BLACK
key foo: WHITE
key foo: BLACK
key foo: BLACK
key foo: WHITE
key foo: BLACK
key foo: WHITE
key foo: WHITE
key foo: BLACK
key foo: WHITE
key foo: BLACK
CL-USER>
So, the code works fine. But I get lots of warnings that I just don't understand. Which previous definition?? I made sure to delete any FASL files I may have laying around, but this seems to always happen anyway.
If I run this directly from the command line with --script, it's fewer warnings:
➜ tests sbcl --script checking.lisp
key foo: BLACK
; file: /Users/renatoathaydes/programming/projects/format-ansi/tests/checking.lisp
; in: LOOP FOR
; (P1:DO-IT :FOO
; (IF P2::FLAG
; :WHITE
; :BLACK))
;
; caught WARNING:
; :FOO is not a known argument keyword.
;
; compilation unit finished
; caught 1 WARNING condition
key foo: WHITE
key foo: BLACK
key foo: WHITE
key foo: BLACK
key foo: BLACK
key foo: WHITE
key foo: BLACK
key foo: WHITE
key foo: WHITE
key foo: BLACK
key foo: WHITE
key foo: BLACK
In my real application, I get no warning when calling a similarly defined function like this:
(format-ansi T "Basic examples:" :bg-color :black)
But on this code just below that:
(loop for i from 0 to 10
do (loop for j from 0 to 10
for flag = (evenp i) then (not flag)
do (format-ansi T " " :bg-color (if flag :white :black)))
(newline))
It says:
; in: LOOP FOR
; (FORMAT-ANSI T " " :BG-COLOR
; (IF FLAG
; :WHITE
; :BLACK))
;
; caught WARNING:
; :BG-COLOR is not a known argument keyword.
;
; compilation unit finished
What's going on here?
(declaim (ftype (function (&key (:foo keytype))) do-it))?