This is the mail archive of the guile@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]

"Magic" Variables?



>Is there any way in Guile to have special code executed whenever a
>particular variable is set? I'm pretty sure there isn't, but I wanted
>to make sure.

There isn't.

I tend to feel that such things are an attempt to make notation more
"cute" than is really a good idea.  If something looks like a variable
assignment, it should be a variable assignment, not a function call.
A programmer shouldn't be wrong if she assumes that assignments are
idempotent, etc.

Tcl variables are an interesting case.  One approach would be able to
map Tcl variables onto real objects that have "-ref" and "-set!"
operations, which could be noticed, traced, etc.  This would help with
implementing aliasing hacks like Tcl's "global" and "upvar", too.

proc datapoint {x} {
  global sigma n
  set sigma [expr $x + $sigma]
  incr n
}

(define (datapoint x)
  (let ((sigma (get-tcl-global 'sigma))
	(n (get-tcl-global 'n)))
    (tcl-var-set! sigma (tcl-+ x sigma))
    (tcl-incr n)))