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

Gor*_*Zar 2 sbcl common-lisp typechecking compiler-warnings

我很难搞清楚如何告诉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

jki*_*ski 6

声明函数的ftype时,可以为rest参数指定类型.

(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
Run Code Online (Sandbox Code Playgroud)