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