1

This is the situation: I have a class definition whose public methods are defined using macro's. The methods look something like this:

<-- macro definition surrounds this method -->
(define/public (message-identifier parameters state-values)
    message-body ...) ...

where all variables (message-identifier, parameters, and state-values) are part of the macro expansion. message-bodyis a sequence of expressions like you would expect in a regular method body (generated by the macro expansion). The body of the method may contain some forms that also expand, for example, if you would want to explicitly state return values, the expression (return 1) may be part of the body, to which we could apply (for example) this rule:

(define-syntax-rule (return value)
    (displayln "i should return a value!"))

I would like to know in the definition of the class method when, in this case, a return statement is included in the body of the method. So could I for example dynamically bind a variable? This example does not work, but it brings accross the idea.

(define/public (message-identifier parameters state-values)
    (define return-included? #t)
    message-body ...) ...

(define-syntax-rule (return value)
    (begin (set! return-included? value) ; 'return-included?' is undefined
           (displayln "i should return a value!")))

Or could I maybe do fancy stuff with macros in the macro that expands the body of the method, instead of having a separate macro for the return statement?

Thank you

1 Answer 1

2

There are several ways to accomplish this, but one way is to use parameters.

You can define a parameter for the return inclusion somewhere where your macro definition can see it:

(define return-included? (make-parameter #f))

then in your code you can have

(define/public (message-identifier parameters state-values)
  (parameterize ([return-included? #f])
    message-body ...))

(define-syntax-rule (return value)
  (begin (return-included? value)
         (displayln "i should return a value!")))

which will work if you consult the value of return-included? in the body of the message-identifier method (the value will be reset outside of the body because of the parameterize). Because of the parameterize, the return information will be set local to each of the methods and won't interfere with other methods.


You can probably also solve this with syntax parameters but that's more complicated. Unless performance is a concern here, parameters are simpler.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.