Hi,

Maxime Devos <maximede...@telenet.be> skribis:

> I wondered if some kind of 'lexical use-modules' was possible, with
> sufficient macroology and module reflection, and it looks like it is:

I agree it would be useful.

Just yesterday wrote this (my goal here was to allow dynamic module
loading based on some condition);

--8<---------------cut here---------------start------------->8---
(define-syntax with-modules
  (syntax-rules ()
    "Dynamically load the given MODULEs at run time, making the chosen
bindings available within the lexical scope of BODY."
    ((_ ((module #:select (bindings ...)) rest ...) body ...)
     (let* ((iface (resolve-interface 'module))
            (bindings (module-ref iface 'bindings))
            ...)
       (with-modules (rest ...) body ...)))
    ((_ () body ...)
     (begin body ...))))
--8<---------------cut here---------------end--------------->8---

… which can be used like this:

--8<---------------cut here---------------start------------->8---
(with-modules (((fibers) #:select (spawn-fiber sleep))
               ((fibers channels)
                #:select (make-channel put-message get-message)))
  (let ((channel (make-channel)))
    (spawn-fiber
      …)))
--8<---------------cut here---------------end--------------->8---

Unlike ‘use-modules’, its clearly limited to the lexical scope of its
body.

IWBN to have a similar functionality in Guile proper, and it could
probably be made more efficient.

Ludo’.

Reply via email to