Common Lisp exporting symbols from packages

前端 未结 4 2073
甜味超标
甜味超标 2021-02-13 05:40

Is there a short way of exporting all the symbols from a package or is it the only way to do it in defpackage. I generally write my code in a file foo.lisp

相关标签:
4条回答
  • 2021-02-13 06:05

    Once the package is created, and all symbols in it created, e.g., by loading your code that implements the package, you can export any symbols you like, e.g., to export all:

    (do-all-symbols (sym (find-package :foo)) (export sym))
    

    You'll probably be happier with

    (let ((pack (find-package :foo)))
      (do-all-symbols (sym pack) (when (eql (symbol-package sym) pack) (export sym))))
    

    which won't try re-exporting everything from used packages.

    0 讨论(0)
  • 2021-02-13 06:05

    There is a way with the cl-annot package. Its export-slots, export-accessors, export-constructors allow to automatically export them. It works for classes and structures.

    For example,

    @export-accessors
    (defclass foo ()
         ((bar :reader bar-of)
          (bax :writer bax-of)
          (baz :accessor baz-of)))
    

    is equivalent to

    (progn
      (export '(bar-of bax-of baz-of))
      (defclass foo ()
         ((bar :reader bar-of)
          (bax :writer bax-of)
          (baz :accessor baz-of))))
    
    0 讨论(0)
  • 2021-02-13 06:06

    Evaluating the macroexpanded code, I get an error for the last nil in the defclass form if no class option is supplied and additional errors as the symbols of the export function have to be quoted. Here is a corrected version which seems to work on my common lisp system (sbcl):

    (defmacro def-exporting-class (name (&rest superclasses) (&rest slot-specs)
                                   &optional class-option)
      (let ((exports (mapcan (lambda (spec)
                               (when (getf (cdr spec) :export)
                                 (let ((name (or (getf (cdr spec) :accessor)
                                                 (getf (cdr spec) :reader)
                                                 (getf (cdr spec) :writer))))
                                   (when name (list name)))))
                             slot-specs)))
        `(progn
           (defclass ,name (,@superclasses)
             ,(append 
               (mapcar (lambda (spec)
                         (let ((export-pos (position :export spec)))
                           (if export-pos
                           (append (subseq spec 0 export-pos)
                               (subseq spec (+ 2 export-pos)))
                           spec)))
                   slot-specs)
               (when class-option (list class-option))))
           ,@(mapcar (lambda (name) `(export ',name))
                     exports))))
    
    
    (macroexpand-1
     '(def-exporting-class test1 nil
       ((test-1 :accessor test-1 :export t)
        (test-2 :initform 1 :reader test-2 :export t)
        (test-3 :export t))))
    
    (PROGN
     (DEFCLASS TEST1 NIL
               ((TEST-1 :ACCESSOR TEST-1) (TEST-2 :INITFORM 1 :READER TEST-2)
                (TEST-3)))
     (EXPORT 'TEST-1)
     (EXPORT 'TEST-2))
    
    0 讨论(0)
  • 2021-02-13 06:16

    Vsevolod's post inspired me to post a macro as well:

    (defmacro defpackage! (package &body options)
      (let* ((classes (mapcan 
                        (lambda (x) 
                          (when (eq (car x) :export-from-classes)
                            (cdr x)))
                        options))
             (class-objs (mapcar #'closer-common-lisp:find-class classes))
             (class-slots (mapcan #'closer-mop:class-slots class-objs))
             (slot-names (mapcar #'closer-mop:slot-definition-name class-slots))
             (slots-with-accessors
               (remove-duplicates (remove-if-not #'fboundp slot-names))))
        (setf options (mapcar
                        (lambda (option)
                          (if (eq (car option) :export)
                            (append option 
                                    (mapcar #'symbol-name slots-with-accessors))
                            option))
                        options))
        (setf options (remove-if 
                        (lambda (option)
                          (eq (car option) :export-from-classes))
                        options))
        `(defpackage ,package ,@options)))
    

    To use:

    CL-USER> 
    (defclass test-class ()
      ((amethod :accessor amethod :initarg :amethod :initform 0)
       (bmethod :reader bmethod :initform 1)))
    #<STANDARD-CLASS TEST-CLASS>
    CL-USER> 
    (closer-mop:ensure-finalized  (find-class 'test-class))
    #<STANDARD-CLASS TEST-CLASS>
    CL-USER> 
    (macroexpand-1 
      `(defpackage! test-package
         (:export "symbol1")
         (:export-from-classes test-class)))
    (DEFPACKAGE TEST-PACKAGE
      (:EXPORT "symbol1" "AMETHOD" "BMETHOD"))
    T
    CL-USER> 
    

    This isn't well tested, and I'm still learning the MOP API, so there may be much better/cleaner ways to achieve the same goal here (especially the fboundp kludge). Also, this only looks for accessor functions on a class. There are also methods that specialize on a class. You could use the MOP to find those as well...

    0 讨论(0)
提交回复
热议问题