(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.
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 withdefine-syntax
.It seems you are using
my-class
to expand the definition of the "method"bigger-x
, which is a function that compares some valuex
to an argument. In this case thecond
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 whatx
might be (and will raise an exception if no variablex
is found at run-time), and the way thatmy-class
is defined will require that you always pass(method (bigger-x other) (> x (other 'x)))
(or a similar syntax, sincemethod
,bigger-x
, etc will be bound as variables) for it to match a valid syntax.