【问题标题】:Lisp SBCL declare a function argument to be a list of a certain type for type checkingLisp SBCL 将函数参数声明为特定类型的列表以进行类型检查
【发布时间】:2017-03-24 23:27:20
【问题描述】:

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

基本上,我想变成这样:

(defun g (f1 &rest fn) (声明(函数 f1)(列表 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

【问题讨论】:

    标签: common-lisp compiler-warnings sbcl typechecking


    【解决方案1】:

    您可以在声明函数的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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多