guile-user
[Top][All Lists]
Advanced

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

Re: intern a top level variable


From: Mortimer Cladwell
Subject: Re: intern a top level variable
Date: Thu, 17 Aug 2023 02:07:41 -0400

> 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
>
>


reply via email to

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