Hi Schemers,
I have a very simple macro question. I am trying to add a new syntax
to scheme in the context of a object system (for object oriented
programming using scheme). Basically, I want to be able to do the
following:
(defclass Window MyObject) ; this declares a new class Window
with superclass MyObject
and then be able to write
(Window
<expr>) ; here <expr> mean any s-expression
where <expr> will be evaluated with two additional local bindings
available: ContextClass and ContextSuperClass, like if there was a
(let ((ContextClass 'Window)
(ContextSuperClass 'MyObject))
<expr>)
For example:
(Window
(+ 1 1)) --> 2
but
(Window
ContextSuperClass) --> MyObject
I found a way of doing it with hygienic macros but I don't like it
(seems much too long for such a simple thing) :
(define-syntax defclass
(syntax-rules ()
((defclass className superClass)
(do-defclass (quote className) (quote superClass)))
((defclass className)
(do-defclass (quote className) 'Object))))
(define (do-defclass className superClass)
(eval
`(define-syntax ,className
(syntax-rules (ContextClass ContextSuperClass)
((className
<expr>
...)
(eval
(quote
(let ((ContextClass (quote ,className))
(ContextSuperClass (quote ,superClass)))
<expr>
...))))))))
Can someone more experimented point me to that (one line) aesthetic
implementation?
Thank you,
Francois Magnan