This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: tie variable
Thierry Bezecourt <thbz@worldnet.fr> writes:
> >I have a question of how to make a special variable in Scheme
> >that is like a tie variable in Perl. Is there any way to call
> >a certain function whenever a variable is referred or modified?
> Just an idea : maybe Goops could be used here ? Variables imported from
> Lisp could be represented in Scheme as instances of some <lisp:variable>
> class. Then you could redefine set! to do whatever you want on
> <lisp:variable> objects. A Goops class and a set of generic methods (to set
> a variable, print it, etc) may help in mapping Lisp data types to Scheme
> data types.
One problem of doing this would be it should work well to set the value,
but not to get it. I do not want to imagine defining generic methods
for every function that might handle lisp values.
Eric Moore <moore@chem.cmu.edu> writes:
> Well, you could do:
>
> (defmacro import-global (sym)
> `(define ,sym
> (make-procedure-with-setter (lambda () (global-ref (quote ,sym)))
> (lambda (x) (global-set! (quote ,sym) x)))))
(snip)
> guile> (display (user-full-name))
> Eric Moore
> guile> (set! (user-full-name) "Eric the Moore, Squire of the Lambda Calculus")
> guile> (display (user-full-name))
> Eric the Moore, Squire of the Lambda Calculus
This seems nice... Although this is a little bit different from
what I wrote, defining it as a procedure would be more natural
since those variables are not quite the same as other variables.
Using it as a procedure will remind programmers what they are doing.
I'll use this notation for both Lisp and Scheme functions. Thanks.
Michael Livshin <mlivshin@bigfoot.com> writes:
> Guile has a macro system called Syntax Case. one of the SC's features
> is "symbol macros". so:
>
> (define-syntax magic-var
> (syntax-case ()
> (magic-var (get-magic-var))))
>
> and you also make sure that `get-magic-var' is a procedure-with-setter.
This is cool... So I could combine GOOPS's generic method and
this define-syntax in order to accomplish my original intention.
But now I think using procedures is a better solution of this
as described above. Thanks anyway.
Lalo Martins <lalo@hackandroll.org> writes:
> > (define user-full-name (global-variable 'user-full-name))
>
> In the final version, you will provide a macro to do this,
> won't you? Let the user think he's doing something like
> (import-variable user-full-name)
Well, now I have emacs-import-variable, emacs-import-function,
and so on. One problem of defining them as macros would be
Lisp allows a programmer to use the same name for a variable and
a function. That is, if there are Lisp definitions as follows
(defvar foo "something")
(defun foo () foo)
one cannot import these two like this
(emacs-import-variable foo)
(emacs-import-function foo)
since the latter expression overrides the former one in Scheme.
We could resolve this in one of the following ways:
(define foo (emacs-import-variable 'foo)
(define get-foo (emacs-import-function 'foo))
as now I do, or
(emacs-import-variable foo)
(emacs-import-function foo get-foo)
I guess the latter looks nice. But I think I'd better choose this
carefully since this part of syntax may remain in the future.
The following notation may be even better
(lisp-import-variable foo)
(lisp-import-function foo get-foo)
or
(import-lisp-variable foo)
(import-lisp-function foo get-foo)
since the program is going to import them from the lisp environment,
not from the Emacs environment. I'll rename this. Any comment?
Thanks,
Keisuke Nishida