cl-cookbook icon indicating copy to clipboard operation
cl-cookbook copied to clipboard

Export tricks (like exporting all accessors of a class at once)

Open Ambrevar opened this issue 6 years ago • 3 comments

There is a surprising among of tricks to know when it comes to exporting symbols in Common Lisp. I think they should be made more obvious in the "Package" chapter.

  • SLIME has C-c x to export the symbol at point, or even the class and all its accessors with slime-export-class. It puts the exports in the :export field of the defpackage.

  • Keeping exports in the defpackage has the drawback that they might run out of sync, for instance if the definition is rename or removed (then we export a symbol that points to nothing). This is an extra maintenance burden. A solution to this is to export at definition site:

(export 'foo)
(defun foo () ...)

This way if foo is rename or remove, the export won't be forgotten.

If typing foo twice really bugs you, you can use the cl-annot library. Where it really shines is that it can export all class accessors for you, which is not doable automatically with (export...). Example:

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

Ambrevar avatar Aug 27 '19 09:08 Ambrevar

Something else important: When (export ...) is used together with defpackage (or maybe even cl-annot), SBCL will complain when reloading the package that the symbol has already been exported, and fail.

The solution to this might be to use uiop:define-package instead of defpackage.

Ambrevar avatar Aug 27 '19 09:08 Ambrevar

https://github.com/atlas-engineer/next/commit/2002a4a#

Ambrevar committed on 20 Sep 2019

;; cl-annot does not call `export' at compile time, which fails with ASDF's ;; "compile-bundle-op" (which is needed for Guix). ;; See https://gitlab.common-lisp.net/asdf/asdf/issues/11.

;; cl-annot is no longer maintained, so we hot-patch it here.

Do you still recommend cl-annot?

vindarel avatar Feb 25 '20 14:02 vindarel

Apparently it works for this user: https://old.reddit.com/r/lisp/comments/f7k8r5/enable_clannot_for_all_files_in_an_asdf_project/.

It breaks *-edit-definition for me, so I would not personally recommend.

Ideally, I would recommend Serapeum once https://github.com/ruricolist/serapeum/issues/38 is fixed.

Ambrevar avatar Feb 25 '20 14:02 Ambrevar