unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* A lexical use-modules?
@ 2022-03-26 19:21 Maxime Devos
       [not found] ` <CAGua6m33_=0=B9jaL4Jw7yP=yycD0eFxiAMK62iVBPuUV8F2wg@mail.gmail.com>
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Maxime Devos @ 2022-03-26 19:21 UTC (permalink / raw)
  To: guile-devel, guix-devel

[-- Attachment #1: Type: text/plain, Size: 1539 bytes --]

Hi,

[ CC'ing guix-devel@ because this functionality could be useful in Guix
package definitions ]

Currently, use-modules cannot be meaningfully used inside procedures,
unless '(current-module)' is always the module in which the procedure
is defined.

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

(use-modules (srfi srfi-1))
(define-syntax use-module/lexical
  ;; todo: integrate into (use-modules ...)?
  (lambda (s)
    (syntax-case s ()
      ((_ foo)
       (let* ((module-name (syntax->datum #'foo))
              (interface (resolve-interface module-name)))
         (define (binding->import name variable)
           (define name-syntax (datum->syntax s name))
           #`(define-syntax #,name-syntax (identifier-syntax (@ foo
#,(datum->syntax #'irrelevant name)))))
         #`(begin #,@(module-map binding->import interface)))))))

(define &exception 'top-level)
(let ()
  (use-module/lexical (ice-9 exceptions))
  (pk 'inner &exception raise-continuable))
;;; (inner #<record-type &exception> #<procedure raise-continuable (obj)>)

(pk 'outer &exception)
;;; (outer top-level)

(pk 'unbound-variable raise-continuable)
;; a backtrace!

Limitation: things like

(define (foo)
  (use-module/lexical (platform-specific-constants))
  (if on-linux?
      CONSTANT_ONLY_DEFINED_ON_LINUX
      CONSTANT_ONLY_DEFINED_ON_HURD))

won't work in cross-compilation contexts.

Greetings,
Maxime.

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-03-29 14:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-26 19:21 A lexical use-modules? Maxime Devos
     [not found] ` <CAGua6m33_=0=B9jaL4Jw7yP=yycD0eFxiAMK62iVBPuUV8F2wg@mail.gmail.com>
     [not found]   ` <4fb1eef748667d5e33d9df674af77ff52d30cd00.camel@telenet.be>
     [not found]     ` <CAGua6m1eNt5ABwDGT_3ifwCjT5YQkpd8W5XBjgwYGZE2B8WW0g@mail.gmail.com>
2022-03-27 10:40       ` Maxime Devos
     [not found]         ` <CAGua6m0H1CZOpDtCUgX=+h6P+zT74_VEA8etQKKs5JwVGLurGQ@mail.gmail.com>
2022-03-27 15:11           ` Maxime Devos
2022-03-29 13:20 ` Ludovic Courtès
2022-03-29 14:59 ` Maxime Devos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).