Cross-package defgeneric / defmethod in Common Lisp?

What is the correct way to determine the common in package A and provide a method for that common in package B in CLOS?

Thank you in advance!

Example:

(defpackage :common (:use :cl)) (in-package :common) (defgeneric compare (ab)) (defmethod compare ((a number) (b number)) (cond ((< ab) -1) ((= ab) 0) (T 1))) (defpackage :a (:use :cl)) (in-package :a) (defclass foo (ab)) (defmethod compare ((x foo) (y foo)) ...) ; SBCL isn't able to access this method via the common package 
+4
source share
1 answer

Methods and functions do not apply to packages. Symbols belong to packages.

 (defpackage :common (:use :cl)) (in-package :common) (defgeneric compare (ab)) (defmethod compare ((a number) (b number)) (cond ((< ab) -1) ((= ab) 0) (T 1))) (defpackage :a (:use :cl)) (in-package :a) (defclass foo (ab)) 

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

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

If COMPARE is exported from the COMMON package, you can write:

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

If COMPARE is exported from the COMMON package, and package A will use the COMMON package, you can write:

 (defmethod compare ((x foo) (y foo)) ...) 
+8
source

Source: https://habr.com/ru/post/1299665/


All Articles