common-lisp - Lisp SBCL 将函数参数声明为某种类型的列表,以进行类型检查

标签 common-lisp compiler-warnings sbcl typechecking

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

相关文章:

format - 如何将参数格式化为函数

lisp - 如何显示前 N 个自然数,了解 Lisp 中的除数

linker - 编译时如何抑制 "linker file unused"

c++ - Visual Studio 2015 - 编译器警告(2 级)C4146

compilation - 如何将参数传递给保存的 SBCL 核心?

lisp - 为什么 CLISP 不能调用某些具有非内部名称的函数?

lisp - 编写一个 lisp 函数 triangle 以奇数作为参数 (n) 并显示打印奇数的三角形

scheme - 将 The Little Schemer 中的 Q 和 P 函数翻译成 Common Lisp?

java - Eclipse(Java): Central "treat warnings as errors"

performance - Lisp:衡量函数的性能