From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?utf-8?Q?Ludovic_Court=C3=A8s?= Newsgroups: gmane.comp.gnu.guix.devel,gmane.lisp.guile.devel Subject: Re: A lexical use-modules? Date: Tue, 29 Mar 2022 15:20:48 +0200 Message-ID: <87wngcyjwv.fsf@gnu.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34681"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cc: guix-devel@gnu.org, guile-devel@gnu.org To: Maxime Devos Original-X-From: guix-devel-bounces+gcggd-guix-devel=m.gmane-mx.org@gnu.org Tue Mar 29 15:21:09 2022 Return-path: Envelope-to: gcggd-guix-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nZBmX-0008qW-GM for gcggd-guix-devel@m.gmane-mx.org; Tue, 29 Mar 2022 15:21:09 +0200 Original-Received: from localhost ([::1]:54554 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nZBmW-0007Qd-8j for gcggd-guix-devel@m.gmane-mx.org; Tue, 29 Mar 2022 09:21:08 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:52298) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nZBmE-0007PV-Ma; Tue, 29 Mar 2022 09:20:50 -0400 Original-Received: from [2001:470:142:3::e] (port=35548 helo=fencepost.gnu.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nZBmE-0002ZD-B7; Tue, 29 Mar 2022 09:20:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:In-Reply-To:Date:References:Subject:To: From; bh=ZpFtBoaK+hRqvnfb/sKolhKyt0jkGNn2GXHT+62daGI=; b=UFFNfNlbR5Cvo1V2Ymwb RgA1w2Fghb6qrUSHv3ol06t5mOnEIU8HLP16PYFhSngT4bRY9pbcxB6RMoQEX5+BhruqgbHhq6Glo k3FLdTKrTLSDIQoZeKe+MBIGHCCfDQkGZUYBoBteqmkQE4H33izbSNCG3OLIU+CAHCZGxM0wTTauq y/T5+k3ADriXgFbULMy6VP8yLeOfqwwP+ZCOJxgRkJZ2EVEOIXIXiMlcguvMkUGys38mgUAnK2cUm EquqjvSZsh0nrCt4w0TKrmXf8N2o0zJ94ucEL6vheeWa8qP7bT4on+g2AXtYKEA7eHdZTsda7ICsG UjBE1zrwAFEtkQ==; Original-Received: from [193.50.110.177] (port=51532 helo=ribbon) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nZBmD-0004oT-SJ; Tue, 29 Mar 2022 09:20:50 -0400 X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 9 Germinal an 230 de la =?utf-8?Q?R=C3=A9volution?= X-PGP-Key-ID: 0x090B11993D9AEBB5 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 3CE4 6455 8A84 FDC6 9DB4 0CFB 090B 1199 3D9A EBB5 X-OS: x86_64-pc-linux-gnu In-Reply-To: (Maxime Devos's message of "Sat, 26 Mar 2022 20:21:07 +0100") X-BeenThere: guix-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane-mx.org@gnu.org Original-Sender: "Guix-devel" Xref: news.gmane.io gmane.comp.gnu.guix.devel:62018 gmane.lisp.guile.devel:21182 Archived-At: Hi, Maxime Devos 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--- =E2=80=A6 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 =E2=80=A6))) --8<---------------cut here---------------end--------------->8--- Unlike =E2=80=98use-modules=E2=80=99, its clearly limited to the lexical sc= ope of its body. IWBN to have a similar functionality in Guile proper, and it could probably be made more efficient. Ludo=E2=80=99.