Jon*_*Yun 5 lisp types runtime function common-lisp
(deftype binary-number-func ()
`(function (number number) number))
(declaim (ftype binary-number-func my-add))
(defun my-add (a b)
(+ (the number a) (the number b)))
;; FAIL:
(assert (typep #'my-add 'binary-number-func))
;; Function types are not a legal argument to TYPEP:
;; (FUNCTION (NUMBER NUMBER) (VALUES NUMBER &REST T))
;; [Condition of type SIMPLE-ERROR]
;; FAIL:
(typep #'my-add '(function (number number) number))
;; Function types are not a legal argument to TYPEP:
;; (FUNCTION (NUMBER NUMBER) (VALUES NUMBER &REST T))
;; [Condition of type SIMPLE-ERROR]
Run Code Online (Sandbox Code Playgroud)
有没有办法检查函数值的复合类型?(在 Common Lisp 中,我使用 SBCL sbcl-1.5.0-x86-64-linux)
提前致谢。
由于 Common Lisp 允许您编写函数,即使编译器无法确定函数的最小函数类型,因此编译器并不总是能够检查函数是否具有特定类型。因此,唯一明智的行为是不检查类型。
\n\n其次,这样的最小类型可能不存在。考虑这个函数:
\n\n(defun foo (key)\n (getf '(:red 1 :blue 2 :green 3 :yellow 4) key))\nRun Code Online (Sandbox Code Playgroud)\n\n它有类型(function (T) T)或(function (T) (or null (integer 1 4)))或(function ((member :red :blue :green :yellow)) (integer 1 4))。请注意,第二种和第三种类型都是正确的,但其中一种不是另一种的子类型。
另请注意,要检查上面的第三种类型,需要准确地了解getf在这种情况下不太可能为真,并且在一般情况下根本不会为真。
编译器检查函数类型是可以的,因为编译器可以抱怨或放弃。对于具有完全不同行为的运行时类型检查函数的不同实现来说,这是完全不可移植的。
\n