guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie question: bind a variable on the fly


From: Jon Wilson
Subject: Re: Newbie question: bind a variable on the fly
Date: Sun, 11 Jun 2006 12:42:18 -0500
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050324)

Hi Vincent,
Don't try this just yet. I want other people to give it a look-over and make sure it isn't foolish and dangerous before anybody lets it out in the wild.


This doesn't use the exception, and it doesn't use a c function, and it doesn't do database lookups. However, it can be used anywhere (inside lets and lambdas) to set! a variable that hasn't been defined. It defines the variable at top level, and then sets it wherever you are. Note: it does establish a global binding rather than a local binding. This may or may not be what you want. I think that using this to establish a local binding would be an extremely difficult thing to do, as well as probably a bad idea.

(define-macro (dyn-set! var val)
              `(begin (if (not (defined? (quote ,var)))
                        (primitive-eval `(define ,(quote ,var) #f)))
                      (set! ,var ,val)))

(defined? 'undefined-symbol) ; => #f
;(set! undefined-symbol #t)  Gives an error.
(dyn-set! undefined-symbol #t) ; No error.
(defined? 'undefined-symbol) ; => #t

Regards,
Jon

Vincent De Groote wrote:
Hello,

Is there a way to catch an "unbound-variable" exception, bind the
variable on the fly, and continue execution as if the exception didn't
occurs ?

I'd like to catch this exception in a c function:  the exception context
should be available, to retrieve the variable or function name.  This
handler will lookup the value in a relational database.

Is this possible ?

Thanks for you replies

Vincent De Groote



_______________________________________________
Guile-user mailing list
address@hidden
http://lists.gnu.org/mailman/listinfo/guile-user




reply via email to

[Prev in Thread] Current Thread [Next in Thread]