Lisp SBCL 将函数参数声明为特定类型的列表以进行类型检查

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

我很难弄清楚如何告诉 sbcl 编译器函数的 &rest args 应该是 TYPE 列表。

基本上,我想转成这样:

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

像这样:

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

我想我可以做到这一点:

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

但我必须使用矢量而不是列表。我知道我可以使用谓词 但是它不会在编译时执行,我必须手动抛出错误。

我想做的事情可行吗?

我正在使用 SBCL 1.3.15

您可以在声明函数的 ftype 时指定剩余参数的类型。

(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