This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

reexport-from-module


Hello,

Please find below the macro `reexport-from-module', which I hope can be
included in Guile in some form.  It allows selective variable re-export,
and supports renaming.  A module can use this to form a custom interface
from pre-existing modules.

BTW, is `defmacro' or `define-macro' preferred?  I see both in
ice-9/boot-9.scm.

thi


---------------------------
;; 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)))))