unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Taylan Kammer <taylan.kammer@gmail.com>
To: Mortimer Cladwell <mbcladwell@gmail.com>,
	Jean Abou Samra <jean@abou-samra.fr>
Cc: guile-user@gnu.org, tomas@tuxteam.de
Subject: Re: intern a top level variable
Date: Wed, 16 Aug 2023 22:35:39 +0200	[thread overview]
Message-ID: <8fa88dc6-48ca-900f-6ee8-51b9c0229362@gmail.com> (raw)
In-Reply-To: <CAOcxjM7j+Jo5NP_ZNvmXcVo92_QvGNa8RYv=CLuX8e7Bq94EhQ@mail.gmail.com>

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	other threads:[~2023-08-16 20:35 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 [this message]
2023-08-17  6:07       ` Mortimer Cladwell
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=8fa88dc6-48ca-900f-6ee8-51b9c0229362@gmail.com \
    --to=taylan.kammer@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=jean@abou-samra.fr \
    --cc=mbcladwell@gmail.com \
    --cc=tomas@tuxteam.de \
    /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).