This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Question about macros.


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]