Search code examples
importpackagecommon-lisp

Rename function in a specific package on Common Lisp


I want to use the package cl-ppcre and series directly on my common lisp environment. I use sly, so in my slynkrc I add this code:

(setf (cdr (assoc '*print-length* slynk:*slynk-pprint-bindings*)) 20)
(setf *print-length* 20)
(setf  *evaluator-mode* :interpret)

(ql:quickload '(:alexandria
                :cl-ppcre
                :cl-interpol
                :series
                :cl-actors
                :chanl
                :lparallel))

(eval-when (:compile-toplevel :execute :load-toplevel)
  (series::install))

(defun λ-reader (stream char)
  (declare (ignore char stream))
  'LAMBDA)

(set-macro-character #\λ #'λ-reader)
(use-package :cl-ppcre)
(use-package :cl-interpol)
(interpol:enable-interpol-syntax) 

The problem with this is whith the symbol function split. that is in both packages defined.

#<THREAD "main thread" RUNNING {10005605B3}>:
  USE-PACKAGE #<PACKAGE "CL-PPCRE"> causes name-conflicts in
  #<PACKAGE "COMMON-LISP-USER"> between the following symbols:
    CL-PPCRE:SPLIT, SERIES:SPLIT

See also:

In Scala you can import, Renaming a class, but in this case I can use shadowing import, and import only what I need, what is the best solution for that, and if it is possible to import and rename a function in common lisp


Solution

  • You could maybe do it like this:

    (defun alias% (as symbol &key (package *package*))
      (when (fboundp symbol)
        (setf (symbol-function as) (symbol-function symbol)))
      (when (boundp symbol)
        (setf (symbol-value as) (symbol-value symbol)))
      (setf (symbol-plist as) (symbol-plist symbol))
      ;; maybe also documentation of all types
      (shadowing-import as package))
    
    (defmacro defalias (as symbol &key (package *package*))
      `(eval-when (:compile-toplevel :load-toplevel :execute)
         (alias% ',as ',symbol :package ,package)))
    

    Then you can do:

    (defalias foo cl:list)
    
    (foo 1 2 3)   ; => (1 2 3)