Search code examples
common-lispcompiler-warningssbcltypechecking

Lisp SBCL declare a function argument to be a list of a certain type for type checking


I am having a hard time figuring out how to tell the sbcl compiler that the &rest args to a function ought to be a list of TYPE.

Basically, I want to turn something like this:

    (defun g (f1 &rest fn)
      (declare (function f1) (list fn)) ... )

To something like this:

    (defun g (f1 &rest fn)
      (declare (function f1) (list-of-fixnums-type? fn)) ... )

I figured I could do this:

    (defun g (f1 fn)
      (declare (function f1) (type (vector function) fn) ... )

But I'd have to use a vector instead of a list. I know I can use predicates but then It wouldn't do it at compile time and I'd have to throw an error manually.

Is what I am trying to do possible?

I am using SBCL 1.3.15


Solution

  • You can specify a type for rest arguments when you declare the ftype of the function.

    (declaim (ftype (function (function &rest fixnum) t)
                    foo))
    (defun foo (f &rest nums)
      (funcall f nums))
    
    (defun bar ()
      (foo #'princ 345 -432 23 "45" 54)) ; warning: Constant "45" conflicts with its asserted type FIXNUM