2

このコードは、警告メッセージを除いて、私が望むように機能します。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の親切な返信に応じて編集します。

私はそれを試しましたが、状況は警告から致命的なエラーにエスカレートしました:

 (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)
4

2 に答える 2

4

の正しい引数リストを指定する必要があります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
于 2011-12-15T23:17:32.827 に答える
3

no-applicable-method のメソッドを次のように定義したいと思います。

(defmethod no-applicable-method ((zang (eql #'zang)) &rest args)
   ...)

現状では、すべてのジェネリック関数に適用されるメソッドを宣言しているため、clisp は既に定義されたメソッドを置き換えていることを通知しています。

于 2011-12-15T15:38:27.117 に答える