This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Question about macros.
- To: Janis Dzerins <jonis at mt dot lv>
- Subject: Question about macros.
- From: thi <ttn at mingle dot glug dot org>
- Date: Fri, 14 Jan 2000 12:20:09 -0800 (PST)
- Cc: guile at sourceware dot cygnus dot com
- References: <Pine.LNX.4.10.10001141709130.741-100000@goo.dev.mt.lv>
- Reply-To: ttn at netcom dot com
Janis Dzerins writes:
> I want to make a macro that would create a variable in top level
> environment. The only solution that works for me now is this:
>
> (defmacro make-v (name init) `(eval (define ,name ,init)))
>
> Is there any other way (more correct) to do this? Using eval does not
> seem quite right to me...
>
> (As I understand, (defmacro make-v (name init) `(define ,name ,init)))
> would create that variable in macro's environment, right?)
>
> BTW, I noticed that paren matching has slowed down dramatically and is
> performed when I paste text with mouse into terminal (I'm using
> gnome-terminal).
to be precise, `name' is added to the invocation environment (where
`make-v' is called, not where it is defined). also, i'm not sure it's
safe to think of a "top-level" environment; probably better to think of
the scheme environment that each environment has access to by default.
this particular environment has been bound to `the-scm-module'.
in any case, i think this is what you want:
(defmacro make-v (a b) `(module-define! the-scm-module ',a ,b))
try the following sequence to verify:
$ guile
guile> (current-module)
guile> (defmacro make-v (a b) `(module-define! the-scm-module ',a ,b))
guile> (make-v make-v make-v)
guile> (module-uses (current-module))
guile> (define-module (random other))
guile> (module-uses (current-module))
guile> (make-v zzz 777)
guile> zzz
guile> (define-module (random another))
guile> zzz
you should see 777 from module `(random another)' as well as any other
module you define. `zzz' is effectively "top-level".
cheers,
thi