Why there is only one method in the define-syntax my-class?

58 Views Asked by At
(define-syntax my-class
    (syntax-rules ()
        [(my-class <class-name> (<attr> ...) 
            (method (bigger-x other) (> x (other 'x))))
         (define (<class-name> <attr> ...)
           (lambda (msg)
             (cond [(equal? msg (quote <attr>)) <attr>] ... 
                   [(equal? msg 'bigger-x) (lambda (other) (> x (other 'x)))]
                   [else "Unrecognized message!"])))]))

This is not a good template. But it's good for explaining the syntax of define-syntax. I am confusing why there is only 1 method in the 4th line. Isn't the quote <attr> in the expression [(equal? msg (quote <attr>)) <attr>] also a method? Their structures are quite similar.

1

There are 1 best solutions below

0
On

Isn't the quote <attr> in the expression [(equal? msg (quote <attr>)) <attr>] also a method?

Supposing you mean "function", then yes, but is a function that will be evaluated at run-time. my-class, for example, is a function that will be evaluated at compile-time, because it was defined with define-syntax.

It seems you are using my-class to expand the definition of the "method" bigger-x, which is a function that compares some value x to an argument. In this case the cond will be evaluated at run-time and (quote <attr>) will expand to a symbol if <attr> turns out to be an identifier. Without some context is hard to know what x might be (and will raise an exception if no variable x is found at run-time), and the way that my-class is defined will require that you always pass (method (bigger-x other) (> x (other 'x))) (or a similar syntax, since method, bigger-x, etc will be bound as variables) for it to match a valid syntax.