From: Mortimer Cladwell <mbcladwell@gmail.com>
To: Taylan Kammer <taylan.kammer@gmail.com>, guile-user@gnu.org
Subject: Re: intern a top level variable
Date: Thu, 17 Aug 2023 02:07:41 -0400 [thread overview]
Message-ID: <CAOcxjM4_Db5MGzN2pbXCc30irAEgMy9fshs0BKtCEM1ZfyC=PA@mail.gmail.com> (raw)
In-Reply-To: <8fa88dc6-48ca-900f-6ee8-51b9c0229362@gmail.com>
> is the name of the variable only going to be known when the program
has already begun running
yes
the values of name and data will be substringed from an argument passed to
the method at runtime
Thanks
Mortimer
On Wed, Aug 16, 2023 at 4:35 PM Taylan Kammer <taylan.kammer@gmail.com>
wrote:
> On 16.08.2023 21:17, Mortimer Cladwell wrote:
> > I would like to define a variable within a method but make it available
> to
> > the entire module - globally.
> > Take a string "abc" convert to a variable (symbol??) abc and set to the
> > string value "def".
> > The values of name and data are unknown - they are variable.
> > In this example the variable (symbol??) abc should evaluate to "def".
> >
> > Thanks tomas you set me on the correct path. The following works:
> >
> > (define (test-intern)
> > (let* ((name "abc")
> > (data "def")
> > )
> > (module-define! (current-module) (string->symbol name) data))
> > )
> >
>
> The "normal" way to do something like this would be with a macro that
> takes the
> name of the variable to be bound, like so:
>
> (define-syntax defvar-example
> (syntax-rules ()
> ((_ <name>)
> (define <name> "def"))))
>
> (defvar-example abc)
>
> ;; variable abc has now been set to "def"
>
> Is there any particular reason the name has to come from a string in your
> case?
>
> For example, is the name of the variable only going to be known when the
> program
> has already begun running, and doesn't appear in the initial source code?
> That
> would be one case where you really have to use 'module-define!' to do it.
>
>
> (Note: I use pattern variables like <name> out of personal preference;
> there's no
> special meaning to the <> around the name.)
>
> --
> Taylan
>
>
next prev parent reply other threads:[~2023-08-17 6:07 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-16 14:55 Re: intern a top level variable Mortimer Cladwell
2023-08-16 16:13 ` Jean Abou Samra
2023-08-16 17:58 ` tomas
2023-08-16 19:17 ` Mortimer Cladwell
2023-08-16 20:35 ` Taylan Kammer
2023-08-17 6:07 ` Mortimer Cladwell [this message]
2023-08-20 13:56 ` Jean Abou Samra
-- strict thread matches above, loose matches on Subject: below --
2023-08-16 12:41 Mortimer Cladwell
2023-08-16 14:08 ` tomas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAOcxjM4_Db5MGzN2pbXCc30irAEgMy9fshs0BKtCEM1ZfyC=PA@mail.gmail.com' \
--to=mbcladwell@gmail.com \
--cc=guile-user@gnu.org \
--cc=taylan.kammer@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).