Skip to content

Instantly share code, notes, and snippets.

@m2ym
Created September 11, 2012 10:22
Show Gist options
  • Save m2ym/3697440 to your computer and use it in GitHub Desktop.
Save m2ym/3697440 to your computer and use it in GitHub Desktop.
Hierarchical packages using user-extensible packages
(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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment