在CLOS中定义包中的泛型并在包B中为此泛型提供方法的正确方法是什么?
提前谢谢!
示例:
(defpackage :common (:use :cl))
(in-package :common)
(defgeneric compare (a b))
(defmethod compare ((a number) (b number))
(cond ((< a b) -1)
((= a b) 0)
(T 1)))
(defpackage :a (:use :cl))
(in-package :a)
(defclass foo (a b))
(defmethod compare ((x foo) (y foo)) ...)
; SBCL isn't able to access this method via the common package
答案 0 :(得分:8)
方法和函数不属于包。符号属于包。
(defpackage :common (:use :cl))
(in-package :common)
(defgeneric compare (a b))
(defmethod compare ((a number) (b number))
(cond ((< a b) -1) ((= a b) 0) (T 1)))
(defpackage :a (:use :cl))
(in-package :a)
(defclass foo (a b))
如果A是当前包,那么您需要编写common :: compare来访问包COMMON的非导出符号COMPARE。
(defmethod common::compare ((x foo) (y foo)) ...)
如果已从包COMMON导出COMPARE,那么您可以写:
(defmethod common:compare ((x foo) (y foo)) ...)
如果已从包COMMON导出COMPARE并且包A将'使用'包COMMON,那么您可以写:
(defmethod compare ((x foo) (y foo)) ...)