I am studying the structure of my CL program and now have problems using CLOS when programming in general with packages.
package.lisp
(defpackage :my-project.a (:use :cl) (:export create-my-object my-object ; EXPORT SINGLE SLOTS? my-slot-1 ; my-slot-... ; my-slot-n ; OR EXPORT ALL ACCESSOR-FUNCTIONS? my-slot-1-accessor ; my-slot-n-accessor... )) (defpackage :my-project.b (:use :cl :my-project.a) (:export print-object-slot))
src.lisp
So far, the MY-OBJECT class is defined in MY-PROJECT.A.
(in-package :my-project.a) (defclass my-object () ((my-slot-1 :accessor my-slot-1-accessor :initarg :my-slot-1) ;... more slots ; (my-slot-2 :accessor my-slot-2-accessor :initarg :my-slot-2) ; (my-slot-n :accessor my-slot-n-accessor :initarg :my-slot-n) ))
like some CREATOR function for objects
(defun create-my-object () (make-instance 'my-object :my-slot-1 "string" ;; further slots... ))
The presence of some function, for example. PRINT-OBJECT in package MY-PROJECT.B, which must process the object called by the function
(in-package :my-project.b) (defun print-object-slot (slot-name object) (format nil "slot-value: ~a" (SLOT-VALUE object slot-name)))
Problem
When executing the following code does not work
(in-package :my-project.b) (describe 'my-object) ; works (print-object-slot 'my-slot-1 ; while this works: 'my-project.a:my-slot-1 [if slot is exported] (create-my-object)) ;; ==> slot MY-PROJECT.B:MY-SLOT-1 is missing from the object ;; MY-PROJECT.A:MY-OBJECT
To access my slots programmatically, in this situation I will need to combine the original package name with the name of the slot in order to get / install the slot from external classes ...
My understanding
Accessory functions from CLOS objects are common functions that belong to the package where they were defined through DEFCLASS, in this case: MY-PROJECT.A
In (use-package :my-project.a) in MY-PROJECT.B the exported characters are imported, so DESCRIBE works. But the symbols of universal slot functions are not included.
Consideration: The architecture of the program should NOT be planned for sharing / exporting objects and accessing slots. It is not well designed for mass import / export of slots / accessories-functions.
Consideration: You can create a custom function that receives / sets slots through the slot access function inside your package, so there is only one interface function for export?
My question is:
This way of handling external CLOS objects does not seem to work. How to export / import these access functions in a reasonable way without manually listing each individual slot?
Edit / Solution
My terminology and the use of slots or function accessories are the cause of this problem ( many thanks to @RainerJoswig for cleaning up the terminology ).
I have not used the exported version of the MY-SLOT-1-ACCESSOR function, which will work as expected, but I will need a "bulk export" if I would like to have access to all the slots in every other external package. @sds did an excellent job of showing how to do this, as well as pointing out the general problem of my approach . Thank you very much:)
In my opinion, I wanted to export only the object and get full access to all internal functions. But this is the wrong way for CLOS, since characters and methods do not share direct bindings to the class / object, and I need to adapt the best organization of the code.