Common Lisp 中的跨包 defgeneric/defmethod?
在 CLOS 中,在包 A 中定义泛型并在包 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
What is the right way to define a generic in package A and to provide a method for this generic in package B in CLOS?
Thank you in advance!
Example:
(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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
方法和函数不属于包。符号属于包。
如果A是当前包,则需要编写common::compare来访问COMMON包的非导出符号COMPARE。
如果 COMPARE 已从包 COMMON 导出,则您可以编写:
如果 COMPARE 已从包 COMMON 导出,并且包 A 将“使用”包 COMMON,则您可以编写:
Methods and functions don't belong to packages. Symbols belong to packages.
If A is the current package, then you need to write common::compare to access the non-exported symbol COMPARE of package COMMON.
If COMPARE has been exported from package COMMON, then you could write:
If COMPARE has been exported from package COMMON and package A would 'use' package COMMON, then you could write: