public
Created

Hierarchical packages using user-extensible packages

  • Download Gist
hierarchical-packages.lisp
Common Lisp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
(in-package :cl-user)
 
(defstruct (hierarchical-package (:include sb-package:standard-package)))
 
(defmethod sb-package:find-package (package-designator (package hierarchical-package))
(flet ((find-package-from-string (string)
(when (and (> (length string) 0)
(char= (elt string 0) #\.))
(setq string (concatenate 'string (package-name package) string)))
(call-next-method string package)))
(typecase package-designator
(package package-designator)
(symbol (find-package-from-string (symbol-name package-designator)))
(string (find-package-from-string package-designator))
(character (find-package-from-string (string package-designator)))
(t (error 'type-error
:datum package-designator
:expected-type '(or character package string symbol))))))
 
(let ((package (make-hierarchical-package
:%name "FOO"
:internal-symbols (sb-impl::make-or-remake-package-hashtable 10)
:external-symbols (sb-impl::make-or-remake-package-hashtable 10))))
(sb-impl::initialize-package package :use '(:cl)))
 
(defpackage :foo.bar (:use :cl))
 
(setq *package* (find-package :foo))
 
(print (find-package :.bar))
 
(print (.bar::+ 1 2))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.