unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* intern a top level variable
@ 2023-08-16 12:41 Mortimer Cladwell
  2023-08-16 14:08 ` tomas
  0 siblings, 1 reply; 4+ messages in thread
From: Mortimer Cladwell @ 2023-08-16 12:41 UTC (permalink / raw)
  To: guile-user

Hi,
How do I intern a top level variable? looks like intern-symbol is
deprecated? Is there a substitute? gensym does not seem to be it:

scheme@(guile-user)> (gensym "abc")
$11 = abc2652
scheme@(guile-user)> abc2652
;;; <unknown-location>: warning: possibly unbound variable `abc2652'
ERROR: In procedure module-lookup: Unbound variable: abc2652

Thanks
Mortimer


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: intern a top level variable
  2023-08-16 12:41 intern a top level variable Mortimer Cladwell
@ 2023-08-16 14:08 ` tomas
  0 siblings, 0 replies; 4+ messages in thread
From: tomas @ 2023-08-16 14:08 UTC (permalink / raw)
  To: Mortimer Cladwell; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 883 bytes --]

On Wed, Aug 16, 2023 at 08:41:18AM -0400, Mortimer Cladwell wrote:
> Hi,
> How do I intern a top level variable?

You don't intern variables, you intern symbols (now I guess you
know that, but then: what are you trying to do?)

> looks like intern-symbol is
> deprecated? Is there a substitute? gensym does not seem to be it:

gensym evaluates to an already interned symbol:

  (symbol-interned? (gensym "foo"))
  => #t

> 
> scheme@(guile-user)> (gensym "abc")
> $11 = abc2652

... abc2652 would be an interned symbol here.

> scheme@(guile-user)> abc2652
> ;;; <unknown-location>: warning: possibly unbound variable `abc2652'
> ERROR: In procedure module-lookup: Unbound variable: abc2652

...but it is unbound (i.e. there is no value associated with it). For
this you would have to

  (define abc2652 "foo")

or something similar.

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: intern a top level variable
  2023-08-16 19:17   ` Mortimer Cladwell
@ 2023-08-16 20:35     ` Taylan Kammer
  2023-08-17  6:07       ` Mortimer Cladwell
  0 siblings, 1 reply; 4+ messages in thread
From: Taylan Kammer @ 2023-08-16 20:35 UTC (permalink / raw)
  To: Mortimer Cladwell, Jean Abou Samra; +Cc: guile-user, tomas

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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: intern a top level variable
  2023-08-16 20:35     ` Taylan Kammer
@ 2023-08-17  6:07       ` Mortimer Cladwell
  0 siblings, 0 replies; 4+ messages in thread
From: Mortimer Cladwell @ 2023-08-17  6:07 UTC (permalink / raw)
  To: Taylan Kammer, guile-user

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-08-17  6:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-16 12:41 intern a top level variable Mortimer Cladwell
2023-08-16 14:08 ` tomas
  -- strict thread matches above, loose matches on Subject: below --
2023-08-16 14:55 Mortimer Cladwell
2023-08-16 16:13 ` Jean Abou Samra
2023-08-16 19:17   ` Mortimer Cladwell
2023-08-16 20:35     ` Taylan Kammer
2023-08-17  6:07       ` Mortimer Cladwell

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