lisp - GNU 剪辑 : suppressing warning message about no-applicable-method

标签 lisp common-lisp clisp clos gnu-common-lisp

除了警告消息外,这段代码可以正常工作。在 GNU Common Lisp 中,如何在不抑制其他可能的警告消息的情况下抑制该消息?

 1 (defgeneric zang (x y)
 2   (:documentation "they want you to put documentation here"))
 3 (defmethod zang ((a number) (b string))
 4   (format t "got to zang ((~s number) (~s string))~%" a b))
 5 (defmethod zang ((a integer) (b string))
 6   (format t "got to zang ((~s integer) (~s string))~%" a b)
 7   (when (evenp a)
 8     (format t "passing control to the other guy~%")
 9     (call-next-method (1+ a) "hoo boy")
10     (format t "returned control from the other guy~%")))
11 (defmethod no-applicable-method (zang &rest args)
12   (format t "no applicable method for (zang ~{~s~^ ~})~%" args))
13 (zang 3.5 "hi")
14 (zang 3 "hi")
15 (zang 4 "hi")
16 (zang "hello" "world")
WARNING: Replacing method #<STANDARD-METHOD (#<BUILT-IN-CLASS T>)> in
         #<STANDARD-GENERIC-FUNCTION NO-APPLICABLE-METHOD>
got to zang ((3.5 number) ("hi" string))
got to zang ((3 integer) ("hi" string))
got to zang ((4 integer) ("hi" string))
passing control to the other guy
got to zang ((5 number) ("hoo boy" string))
returned control from the other guy
no applicable method for (zang "hello" "world")

编辑 以回应 Vatine 的友善回复:

我试过了,情况从警告升级为 fatal error :

 (defgeneric zang (x y)
   (:documentation "they want you to put documentation here"))
 (defmethod zang ((a number) (b string))
   (format t "got to zang ((~s number) (~s string))~%" a b))
 (defmethod zang ((a integer) (b string))
   (format t "got to zang ((~s integer) (~s string))~%" a b)
   (when (evenp a)
     (format t "passing control to the next guy~%")
     (call-next-method (1+ a) "hoo boy")
     (format t "returned control from the next guy~%")))
 ;(defmethod no-applicable-method (zang &rest args)
 ;  (format t "no applicable method for (zang ~{~s~^ ~})~%" args))
 (defmethod no-applicable-method ((zang eql #'zang) &rest args)
   (format t "no applicable method for (zang ~{~s~^ ~})~%" args))
 (zang 3.5 "hi")
 (zang 3 "hi")
 (zang 4 "hi")
 (zang "hello" "world")
*** - DEFMETHOD NO-APPLICABLE-METHOD: Invalid specialized parameter in method
      lambda list ((ZANG EQL #'ZANG) &REST ARGS): (ZANG EQL #'ZANG)

最佳答案

您需要为 NO-APPLICABLE-METHOD 提供正确的参数列表。 如果您使用编译器(即使是 CLISP 实现也可以通过 COMPILE-FILE 进行编译),您还应该在编译时收到有关不正确参数列表的错误消息。

例如 LispWorks 编译器说:

**++++ Error between functions:
 An argument is not an atom or list of two elements : (ZANG EQL (FUNCTION ZANG))

固定版本:

(defgeneric zang (x y)
   (:documentation "they want you to put documentation here"))
(defmethod zang ((a number) (b string))
   (format t "got to zang ((~s number) (~s string))~%" a b))
(defmethod zang ((a integer) (b string))
   (format t "got to zang ((~s integer) (~s string))~%" a b)
   (when (evenp a)
     (format t "passing control to the next guy~%")
     (call-next-method (1+ a) "hoo boy")
     (format t "returned control from the next guy~%")))
;(defmethod no-applicable-method (zang &rest args)
;  (format t "no applicable method for (zang ~{~s~^ ~})~%" args))

(defmethod no-applicable-method ((zang (eql #'zang)) &rest args)
   (format t "no applicable method for (zang ~{~s~^ ~})~%" args))

例子:

(defun test ()
 (zang 3.5 "hi")
 (zang 3 "hi")
 (zang 4 "hi")
 (zang "hello" "world"))

CL-USER 1 > (test)
got to zang ((3.5 number) ("hi" string))
got to zang ((3 integer) ("hi" string))
got to zang ((4 integer) ("hi" string))
passing control to the next guy
got to zang ((5 number) ("hoo boy" string))
returned control from the next guy
no applicable method for (zang "hello" "world")
NIL

关于lisp - GNU 剪辑 : suppressing warning message about no-applicable-method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8519816/

相关文章:

routes - Hunchentoot(简单路线)路线出现在单独的项目中吗?

format - Lisp 多次格式化一个字符

lisp - Common Lisp中的整数除法?

Lisp- "[Function name] Is not a Number"

lisp - 第一个更大的(常见的)lisp 程序 -> 'random' 没有按预期工作

字符串列表上的 lisp 成员函数

list - 普通口齿不清 : Removing elements with the same cdr

exception - Common Lisp 错误处理系统中 'store-value' 和 'use-value' 重新启动的语义

scheme - LISP clause for and clause let ¿why?, making a programming language in racket using ragg

写入宏的宏-编译错误