<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">I’m making my own object-copy macro (just like struct-copy, but for objects), and I am getting this weird error:<div><font face="Courier New">. %: identifier used out of context in: %</font></div><div><br></div><div>What am I doing wrong? And is there another way to do it that doesn’t involve an eval-syntax (I suspect that’s part of the problem)? </div><div><br></div><div>And also, is there another version of field-names that takes a class instead of an object, because I think that will create another problem when I get to the last test (with <span style="font-family: 'Courier New';">not-really-chum</span>). </div><div><br></div><div>Here’s the code:</div><div><div><font face="Courier New">#lang racket/base (require (for-syntax racket/base))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(provide object-copy)</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(require racket/class</font></div><div><font face="Courier New"> (for-syntax</font></div><div><font face="Courier New"> syntax/parse))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(module+ test</font></div><div><font face="Courier New"> (require rackunit))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(define-syntax object-copy</font></div><div><font face="Courier New"> (lambda (stx)</font></div><div><font face="Courier New"> (syntax-parse stx</font></div><div><font face="Courier New"> [(object-copy %-expr obj-expr</font></div><div><font face="Courier New"> [field-id:id expr:expr]</font></div><div><font face="Courier New"> ...)</font></div><div><font face="Courier New"> #:declare %-expr (expr/c #'class? #:name "class")</font></div><div><font face="Courier New"> #:declare obj-expr (expr/c #'(is-a?/c %-expr) #:name "object")</font></div><div><font face="Courier New"> (with-syntax ([ooo '...])</font></div><div><font face="Courier New"> #'(let* ([% %-expr.c]</font></div><div><font face="Courier New"> [obj obj-expr.c]</font></div><div><font face="Courier New"> [all-field-syms (field-names obj)]</font></div><div><font face="Courier New"> [remaining-field-syms (remove* '(field-id ...) all-field-syms)])</font></div><div><font face="Courier New"> (with-syntax ([(remaining-field-id ooo) remaining-field-syms])</font></div><div><font face="Courier New"> (eval-syntax</font></div><div><font face="Courier New"> #'(new %</font></div><div><font face="Courier New"> [field-id expr] ...</font></div><div><font face="Courier New"> [remaining-field-id (get-field remaining-field-id obj)] ooo</font></div><div><font face="Courier New"> )))))]</font></div><div><font face="Courier New"> )))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(module+ test</font></div><div><font face="Courier New"> (define fish%</font></div><div><font face="Courier New"> (class object% (init-field color weight)</font></div><div><font face="Courier New"> (super-new) (inspect #f)))</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> (define marlin (new fish% [color 'orange-and-white] [weight 11]))</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> (define dory (object-copy fish% marlin</font></div><div><font face="Courier New"> [color 'blue]))</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> (check-equal? dory (new fish% [color 'blue] [weight 11]))</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> (define shark%</font></div><div><font face="Courier New"> (class fish% (init-field weeks-since-eating-fish)</font></div><div><font face="Courier New"> (super-new) (inspect #f)))</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> (define bruce (new shark% [color 'grey] [weight 110] [weeks-since-eating-fish 3]))</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> (define chum (object-copy shark% bruce</font></div><div><font face="Courier New"> [weight 90]</font></div><div><font face="Courier New"> [weeks-since-eating-fish 0]))</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> (check-equal? chum (new shark% [color 'grey] [weight 90] [weeks-since-eating-fish 0]))</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> (define not-really-chum</font></div><div><font face="Courier New"> (object-copy fish% bruce</font></div><div><font face="Courier New"> [weight 90]))</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> (check-equal? not-really-chum (new fish% [color 'grey] [weight 90]))</font></div><div><font face="Courier New"> </font></div><div><font face="Courier New"> )</font></div></div><div><br></div><div><br></div></body></html>