Eli*_*der 5 lisp static-typing common-lisp
当我在Common Lisp中定义一个函数时,如下所示:
(defun foo (n)
(declare (type fixnum n))
(+ n 42))
Run Code Online (Sandbox Code Playgroud)
我期待一个电话会(foo "a")
立即失败,但它会在通话中失败+
.是declare
形式不保证静态类型检查?
Mat*_*ard 14
传统上,类型声明旨在用作编译器的保证以用于优化目的.对于类型检查,请使用check-type
(但请注意,它也在运行时进行检查,而不是在编译时进行检查):
(defun foo (n)
(check-type n fixnum)
(+ n 42))
Run Code Online (Sandbox Code Playgroud)
也就是说,不同的Common Lisp实现以不同的方式解释类型声明.例如,如果策略设置足够高,SBCL 会将它们视为要检查的类型safety
.
此外,如果你想要静态检查,SBCL也可能是你最好的选择,因为它的类型推理引擎会警告你遇到的任何不一致.为此,ftype
可以充分利用声明:
CL-USER(1): (declaim (ftype (function (string) string) bar))
CL-USER(2): (defun foo (n)
(declare (type fixnum n))
(bar n))
; in: DEFUN FOO
; (BAR N)
;
; caught WARNING:
; Derived type of N is
; (VALUES FIXNUM &OPTIONAL),
; conflicting with its asserted type
; STRING.
; See also:
; The SBCL Manual, Node "Handling of Types"
;
; compilation unit finished
; caught 1 WARNING condition
FOO
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2277 次 |
最近记录: |