I'm sorry if this question is frightfully naive...
How does one pass a variable to a common lisp function, in
such a way that the function can change that variable?
Is this contrary to common usage? Are global variables
the only way to get this effect?
My situation: I have a gui clos object, scrollbar-and-readout,
and I want any change to the scrollbar to automatically affect
the value of a variable of my choice.
An alternative, I suppose, is for me to always call
(setq var (get-value scrollbar-and-readout))
before I use <var>. But it seems cleaner to have the updating
done automatically -- similar in style to Smalltalk's model-view-
controller.
I have the uneasy feeling that my years of programming in C are
leading me to miss an obvious solution here... :-)
Any advice?
- Paul Shannon
········@nrao.edu
National Radio Astronomy Observatory
Charlottesville, Virginia
On 21 Mar 1995, Paul Shannon wrote:
> How does one pass a variable to a common lisp function, in
> such a way that the function can change that variable?
> Is this contrary to common usage? Are global variables
> the only way to get this effect?
>
> My situation: I have a gui clos object, scrollbar-and-readout,
> and I want any change to the scrollbar to automatically affect
> the value of a variable of my choice.
>
> An alternative, I suppose, is for me to always call
>
> (setq var (get-value scrollbar-and-readout))
>
> before I use <var>. But it seems cleaner to have the updating
> done automatically -- similar in style to Smalltalk's model-view-
> controller.
The scrollbar is the controller, its graphic representation is the view.
The model is the thing it is controlling, which you have alluded to as
being a variable value. It appears that you could benefit from making
the model into a CLOS object and designating a protocol for accessing /
updating the controlled value(s). Analogous to a Smalltalk ValueModel.
From: Barry Margolin
Subject: Re: getting the effect of pass-by-reference
Date:
Message-ID: <3kn5c8$i33@tools.near.net>
In article <······················@iapetus.cv.nrao.edu> ········@iapetus.cv.nrao.edu (Paul Shannon) writes:
>How does one pass a variable to a common lisp function, in
>such a way that the function can change that variable?
>Is this contrary to common usage? Are global variables
>the only way to get this effect?
They're the simplest way. Another common way is to use a macro, which
contains a SETQ that updates the variable.
But probably the best way is to have the function take a functional
argument rather than a variable name, and you pass it a function that does
the update.
>My situation: I have a gui clos object, scrollbar-and-readout,
>and I want any change to the scrollbar to automatically affect
>the value of a variable of my choice.
(defmethod update ((self scrollbar-and-readout)
&optional notify-function)
...
(when notify-function
(funcall notify-function new-value)))
(let ((local-scrollbar-value 0))
...
(set-value my-scrollbar #'(lambda (x) (setq local-scrollbar-value x))))
The advantage of this is that you're not limited only to setting a
variable. The notify function can do whatever it likes with the value: it
can set a variable, store it in a structure, write it to a file, call some
other functions, etc.
If a particular scrollbar will always be associated with the same notify
function, it might be more appropriate to make this a slot in the
scrollbar-and-readout class, rather than a parameter to update, and provide
it as part of the initialization list at make-instance time.
--
Barry Margolin
BBN Planet Corporation, Cambridge, MA
······@bbnplanet.com
In article <······················@iapetus.cv.nrao.edu>,
········@iapetus.cv.nrao.edu (Paul Shannon) wrote:
> My situation: I have a gui clos object, scrollbar-and-readout,
> and I want any change to the scrollbar to automatically affect
> the value of a variable of my choice.
>
> An alternative, I suppose, is for me to always call
>
> (setq var (get-value scrollbar-and-readout))
>
> before I use <var>. But it seems cleaner to have the updating
> done automatically -- similar in style to Smalltalk's model-view-
> controller.
Why set <var>? Just call your get-value method when you need it. If
you'll be using the value multiple times in one function, use a LET form
to bind it to a temporary variable.
If you really have a good reason to update the global variable, add a
method to your scrollbar that updates the variable when the scroller
changes position.
Dave
---
CPU Cycles: Use them now or lose them forever...
http://www.teleport.com/~dlamkins
This is *really* dirty: encorporate the variable as a defstruct member; any changes in the sub-function will change it globally......
This is also how I discovered one can implement a "Binary Tree" in a similar manner to
it's C/pascal counter-part with pointers....
--
From Martin Glanvill.
WWW page:- http://mcg.math.waikato.ac.nz (temp disab.)
_/ _/_/_/ _/ _/ _/ _/ _/ _/
_/ _/ _/_/_/ _/ _/ _/
_/_/_/ _/_/_/ _/ _/ _/_/ _/ _/