Search code examples
packagecommon-lisp

alias package names in Common Lisp


I'm using an external package in Common Lisp for a project; I want to be able to use the package but alias it to a shorter name, similar to how in Clojure I could do

(require '[unnecessarily-long-package-name :as ulpn])

In order to avoid naming conflicts, I'd rather not do this:

(defpackage #:my-package
  (:use #:cl #:other-package))

(in-package :my-package)

(take-over-world "pinky" "brain")

where other-package defines take-over-world. I could just type the full qualified package name every time:

(defpackage #:my-package
  (:use #:cl))

(in-package :my-package)

(other-package:take-over-world "pinky" "brain")

but in my case the package I'm importing has an unnecessarily long name. Is there a way I can use other-package as

(op:take-over-world "pinky" "brain")

by aliasing it to op? I wasn't able to find anything like this in the relevant chapter in Practical Common Lisp.


Solution

  • The way to do it now (since 2018 maybe?) is with Package-Local Nicknames (PLN), now available in most implementations.

    (defpackage :mypackage
      (:use :cl)
      (:local-nicknames (:nickname :original-package-name)
                        (:alex :alexandria)
                        (:re :cl-ppcre)))
    
    (in-package :mypackage)
    
    ;; You can use :nickname instead of :original-package-name
    (nickname:some-function "a" "b")
    

    (edit) And, from anywhere, you can use (uiop:add-package-local-nickname :nickname :package).

    When nicknames were global, PLNs are now local to your package and don't pollute the global namespace.