我很难弄清楚如何告诉 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
关于common-lisp - Lisp SBCL 将函数参数声明为某种类型的列表,以进行类型检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43010732/