Hi all,<br><br>Searching for my way through macros (and seeing some shiny lights ahead), I wanted to make a getter/setter helper for class fields.<br>Here is the setter I came up with:<br><br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">
<span style="font-family: arial,helvetica,sans-serif;">(define-syntax (setter stx)</span><br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;"> (syntax-case stx ()</span><br style="font-family: arial,helvetica,sans-serif;">
<span style="font-family: arial,helvetica,sans-serif;"> [(_ id)</span><br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;"> (with-syntax ([set-id (symbol-append* "set-" #'id)]</span><br style="font-family: arial,helvetica,sans-serif;">
<span style="font-family: arial,helvetica,sans-serif;"> [val (gensym)])</span><br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;"> #'(define/public (set-id val) (set! id val))</span><br style="font-family: arial,helvetica,sans-serif;">
<span style="font-family: arial,helvetica,sans-serif;"> )]</span><br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;"> [(_ id1 id2 ...)</span><br style="font-family: arial,helvetica,sans-serif;">
<span style="font-family: arial,helvetica,sans-serif;"> #'(begin (setter id1)</span><br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;"> (setter id2 ...))]</span><br style="font-family: arial,helvetica,sans-serif;">
<span style="font-family: arial,helvetica,sans-serif;"> ))</span><br style="font-family: arial,helvetica,sans-serif;"><br style="font-family: arial,helvetica,sans-serif;"></blockquote><div><br>I hope I've done things right, otherwise please tell me.<br>
It uses the two below functions: <br><br></div><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote"><span style="font-family: arial,helvetica,sans-serif;">(define-for-syntax (symbol-append* . args)</span><br style="font-family: arial,helvetica,sans-serif;">
<span style="font-family: arial,helvetica,sans-serif;"> (string->symbol </span><br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;"> (apply string-append (map ->string args))))</span><br style="font-family: arial,helvetica,sans-serif;">
</blockquote><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote"><div> </div></blockquote><div><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">
(define-for-syntax (->string x)<br> (cond [(syntax? x) (->string (syntax->datum x))]<br> [else (format "~a" x)]<br> ))<br></blockquote> </div>The setter can be used in a class:<br><br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">
(define a%<br> (class object% (super-new)<br> (init-field x)<br> (setter x)<br> ))<br></blockquote><div></div><br><br>There is one problem though:<br>When I want to use this method inside the class, (send this set-x 3) works perfectly (this kind of call also works outside of the class), but (set-x 3) raises an error, because `set-x' is not bound at expand phase. <br>
<br>My question: What should I modify the setter macro do so that the setter function (like set-x) could be bound at expand-phase? <br><br>Strangely, I have no problem with the getter, because it's seems like the method get-something is already defined for any field.<br>
<br>Thanks,<br>Laurent<br>