Question

I'm writing some code in SBCL, and the ordering of my functions keeps causing warnings of the following type to appear when I load files into the REPL:

;caught STYLE-WARNING:
    undefined function: FOO

Where FOO is the symbol for the function. This is purely due to how they are ordered in my file, as the function FOO is defined, just not before the part of the code that throws that warning.

Now, in Clojure, which is the Lisp I'm familiar with, I have the declare form, which lets me make forward declarations to avoid this kind of issue. Is there something similar for SBCL/Common Lisp in general?

Was it helpful?

Solution 2

Here's what I found in the manual, section 4.1.1:

CL-USER> (defun foo (x) (bar x))
; in: DEFUN FOO
;     (BAR X)
; 
; caught STYLE-WARNING:
;   undefined function: BAR
; 
; compilation unit finished
;   Undefined function:
;     BAR
;   caught 1 STYLE-WARNING condition
FOO
CL-USER> (declaim (sb-ext:muffle-conditions style-warning))
; No value
CL-USER> (defun baz (y) (quux y))
BAZ

So you can at least silence the style warnings.

I also thought about how SBCL is handling the evaluation step in the REPL: it compiles the code. So I restarted the inferior lisp process and ran "compile region" on the following two lines:

(defun foo (x) (bar x))
(defun bar (y) (baz y))

and SBCL only complained about baz, but not about bar. Are you giving single functions to SBCL or larger chunks?

OTHER TIPS

We can use the '(declaim (ftype ...))' for that:

(declaim (ftype (function (integer list) t) ith))

(defun foo (xs)
  (ith 0 xs))

(defun ith (n xs)
  (nth n xs))

Both the function 'foo' and 'ith' works fine and there is not any style warning about that.

http://www.lispworks.com/documentation/HyperSpec/Body/d_ftype.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top