[racket] macro and set! problem

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Sat Jul 10 04:07:07 EDT 2010

I think the issue is that any variable, V, in a module compiled
without a (set! V E) expression somewhere in it is compiled as
immutable.  If the only set! in the module is in a macro that has not
been called yet, your variable is being compiled as immutable.  If you
put a set! of the variable somewhere inside the module -- even just
(set! V V) as the last form in the module -- the variable should be
compiled as mutable and the macro should work in its original form.

I have not tested this, so it may not be right, but it is what I
recall about how set! works with modules.

Carl Eastlund

On Sat, Jul 10, 2010 at 5:52 AM, Skeptic . <skeptic2000 at hotmail.com> wrote:
>
>
> Hi,
> I was trying to come up with a naive implementation of check-expect/test and I found out that if a macro expands to a set! on a module-level variable directly, an error will be given when using the macro from another module. A simple fix seems to instead have the macro to expands to a function call that performs the set!.
> I'm not sure what to think about it and I would like to hear why it works that way or what I have missed.
> Here's what I came up with in a few minutes :
> (provide check-expect test)
> (define tests '())
> (define-syntax check-expect  (λ (stx)    (syntax-case stx ()      [(_ exp res)        #'(add-test (list (delay exp) res #'stx))])))
> (define add-test  (λ (test) (set! tests (cons test tests))))   ; add-test is there because having check-expect expands to set! doesn't work when called from another module.
> (define test  (λ ()    (printf "Ran ~a checks.\n" (length tests))    (let* ([failed (filter (λ (test) (not (equal? (force (car test)) (cadr test)))) (reverse tests))]           [nbfailed (length failed)])      (if (> nbfailed 0)          (begin            (printf "~a tests failed:\n" nbfailed)            (for-each (λ (ft)                         (let ([ft-stx (caddr ft)])                        (printf "Actual value ~a differs from ~a, the expected value.\n ~a\n" (force (car ft)) (cadr ft)                                 (format "In ~a at line ~a column ~a" (syntax-source ft-stx) (syntax-line ft-stx) (syntax-column ft-stx))))) failed))          (printf "All test passed!\n"))      )))


Posted on the users mailing list.