[plt-scheme] Linking Scheme- and C-variables
On Nov 1, Daniel K. Skovenborg wrote:
> I'm making a C-program with embedded MzScheme (used as console) and
> I like to have MzScheme so deeply integrated as possible. Therefore
> I like to have C-variables linked to Scheme-variables so when you
> change the Scheme-variable in the console with set! then the
> C-variable will change too and vice versa.
I don't think that there's a way to do this at the C level, but here
is one way it could be achieved, using a macro to hide the variable
and perform some arbitrary operation when set:
(module hooked-vars mzscheme
(provide def-hooked-var)
(define-syntax (def-hooked-var stx)
(syntax-case stx ()
((_ var val hook) (identifier? #'var)
(with-syntax ((hidden-var
(datum->syntax-object
#'var
(string->symbol
(string-append (symbol->string (syntax-e #'var))
"-hidden-value"))
#'var)))
#'(begin (define hidden-var val)
(define-syntax var
(make-set!-transformer
(lambda (stx)
(syntax-case stx (set!)
((set! var new-val)
#'(set! hidden-var (hook new-val)))
((var . xs) #'(hidden-var . xs))
(var #'hidden-var)))))))))))
For example:
> (require hooked-vars)
> (define a 1)
> (def-hooked-var b 1 (lambda (new) (set! a new) new))
> (set! b 3)
> b
3
> a
3
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://www.barzilay.org/ Maze is Life!