Common Lisp 中的跨包 defgeneric/defmethod?

发布于 2024-08-20 04:30:22 字数 471 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2024-08-27 04:30:22

方法和函数不属于包。符号属于包。

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

如果 COMPARE 已从包 COMMON 导出,则您可以编写:

(defmethod common:compare ((x foo) (y foo)) ...)   

如果 COMPARE 已从包 COMMON 导出,并且包 A 将“使用”包 COMMON,则您可以编写:

(defmethod compare ((x foo) (y foo)) ...)   

Methods and functions don't belong to packages. Symbols belong to packages.

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

If A is the current package, then you need to write common::compare to access the non-exported symbol COMPARE of package COMMON.

(defmethod common::compare ((x foo) (y foo)) ...)   

If COMPARE has been exported from package COMMON, then you could write:

(defmethod common:compare ((x foo) (y foo)) ...)   

If COMPARE has been exported from package COMMON and package A would 'use' package COMMON, then you could write:

(defmethod compare ((x foo) (y foo)) ...)   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文