guix-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A lexical use-modules?


From: Ludovic Courtès
Subject: Re: A lexical use-modules?
Date: Tue, 29 Mar 2022 15:20:48 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)

Hi,

Maxime Devos <maximedevos@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

[Prev in Thread] Current Thread [Next in Thread]