定義されていないスーパクラスメソッドの呼び出しを防ぎたい - lisp-cookbook-ja/common-lisp GitHub Wiki
定義されていないスーパクラスメソッドの呼び出しを防ぎたい
next-method-pを使用するとスーパクラスにメソッドが定義されているかを確認してから呼び出せます。
(defclass one () ())
(defclass two (one) ())
(defmethod meth1 ((c two))
(print "2!")
(and (next-method-p)
(call-next-method)))
(defmethod meth1 ((c one))
(print "1!")
(and (next-method-p)
(call-next-method)))
;=> nil
(meth1 (make-instance 'two))
;-> "2!"
; "1!"
;=> nil
もしくは、定義されていないスーパクラスメソッドを呼び出した場合に呼ばれる no-next-method の動作を定義することでも対処できます。
(defclass one () ())
(defclass two (one) ())
(defmethod meth2 ((c two))
(print "2!")
(call-next-method))
(defmethod meth2 ((c one))
(print "1!")
(call-next-method))
(defmethod no-next-method ((gf (eql #'meth2)) (meth T) &rest args)
(declare (ignore args))
nil)
(meth2 (make-instance 'two))
;-> "2!"
; "1!"
;=> nil