This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: forcing a getter for variable accesses
Michael Livshin <mike@olan.com> writes:
> Greg Badros <gjb@cs.washington.edu> writes:
>
> > Is there any way currently to force access to a variable via a getter
> > procedure? E.g., in Scwm we have user-configurable variables such as
> >
> > *desk-width*
> >
> > that we want people to access by using (optget *desk-width*) instead of
> > just *desk-width*. In this case, the variable has a getter function:
> > (car (desk-size))
> >
> > I'm looking for thoughts about how to force using the getter function
> > w/o requiring all accesses to be inside the `optget' macro. I think
> > this is generally useful for guile, so if it's not already possible,
> > it'd be nice if we could make it possible.
>
> don't export a variable at all, and instead export a function:
>
> (*desk-width*) - gets the value
> (*desk-width* new-value) - sets the value
Of course I considered this but should've spelled out why it's not
desirable. I think it's too hard for newbies-- it's a potentially
confusing bug at every use site-- they end up using the procedure
instead of the value from evaluating the procedure. It also makes code
harder to read, etc. It doesn't seem like it'd be too hard to
implement an extension to guile's evaluator that, say, if a variable was
bound to #<unspecified> and that variable's symbol had an
object-property 'getter (and perhaps the symbol were of the form *foo*),
then invoke the getter property as a procedure and evaluate to the value
the procedure of no arguments evaluates to. I think this kind of thing
would be incredibly useful, if not elegant enough to feel schemey.
Thanks,
Greg