This is the mail archive of the
guile@cygnus.com
mailing list for the Guile project.
interate over the a public interface?
- To: clark.mcgrew@sunysb.edu
- Subject: interate over the a public interface?
- From: thi <ttn@mingle.glug.org>
- Date: Sat, 8 May 1999 03:06:54 -0700 (PDT)
- Cc: guile@cygnus.com
- References: <m10fzEv-000V2QC@boxer.physics.sunysb.edu>
- Reply-To: ttn@netcom.com
Clark McGrew writes:
> I've got an interactive environment that I'd like to include lots of
> small modules into. In otherwords, I'd like to have a module boot
>
> (define-module (boot)
> :use-module (mod1)
> :use-module (mod2)
> :use-module (mod3))
>
> that I can include by saying
>
> (use-module (boot))
>
> and get the exports of mod1, mod2, &c.
>
> I've had some success with module-for-each, but it interates over all
> variables in a module (local and exported). Is there a way to only
> iterate over the public interface?
this is called in my mind the re-export problem. there might be a
canonical name or solution these days (or soon) but for now you can do
something like this:
;; Re-export variables from module named OTHER-MODULE-NAME. If SPECIFICALLY
;; is the empty list, all public variables from OTHER-MODULE-NAME are
;; exported, otherwise, each element in SPECIFICALLY is taken to be either a
;; symbol naming a variable to be exported, or a pair of symbols of the form
;; (OLD-NAME . NEW-NAME) describing the mapping to be used. OLD-NAME should
;; name an exported variable in OTHER-MODULE-NAME.
;;
(defmacro reexport-from-module (other-module-name . specifically)
`(let ((cur-mod (current-module))
(other-mod (resolve-module ',other-module-name))
(spec (map (lambda (x) (if (pair? x) x (cons x x))) ; for assq
',specifically)))
(let ((add! (lambda (old-name new-name)
(module-add! (module-public-interface cur-mod)
new-name
(module-variable other-mod old-name)))))
(module-map (lambda (sym x)
(if (eq? '() spec)
(add! sym sym)
(let ((try (assq sym spec)))
(and try (add! (car try) (cdr try))))))
(module-public-interface other-mod)))))
note that this also handles renaming, which can be useful when the
constituent modules export different procedures or variables but w/ the
same name. such is the job of the shady import/export specialist that
is your top-level module: to launder all bindings into oblivion (insert
international-bank-account-smiley here B-). aren't you glad you're
using guile?
thi,
memorizing 93-digit numbers to the sounds of shredding paper...