(unearthing an old topic to provide an answer)<br><br><div class="gmail_quote">On Wed, Mar 10, 2010 at 13:51, Noel Welsh <span dir="ltr"><<a href="mailto:noelwelsh@gmail.com">noelwelsh@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">On Wed, Mar 10, 2010 at 11:24 AM, Laurent <<a href="mailto:laurent.orseau@gmail.com">laurent.orseau@gmail.com</a>> wrote:<br>
<br>
> Searching for my way through macros (and seeing some shiny lights ahead), I<br>
> wanted to make a getter/setter helper for class fields.<br>
> Here is the setter I came up with:<br>
><br>
>> (define-syntax (setter stx)<br>
>> (syntax-case stx ()<br>
>> [(_ id)<br>
>> (with-syntax ([set-id (symbol-append* "set-" #'id)]<br>
>> [val (gensym)])<br>
>> #'(define/public (set-id val) (set! id val))<br>
>> )]<br>
>> [(_ id1 id2 ...)<br>
>> #'(begin (setter id1)<br>
>> (setter id2 ...))]<br>
>> ))<br>
>><br>
<br>
</div>You don't need the gensym. Hygiene will take of this automatically.<br></blockquote><div><br>(Now I've finally learned that all such identifiers are renamed.)<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
So, the following works:<br>
<div class="im"><br>
(define-syntax (setter stx)<br>
(syntax-case stx ()<br>
[(_ id)<br>
</div> (with-syntax ([set-id (symbol-append* "set-" #'id)])<br>
<div class="im"> #'(define/public (set-id val) (set! id val))<br>
)]<br>
[(_ id1 id2 ...)<br>
#'(begin (setter id1)<br>
(setter id2 ...))]<br>
))<br>
<br>
</div>I don't know how to solve your other problem other than by<br>
hygienically introducing the identifier.<br></blockquote><div><br>I probably didn't understand that last bit of your answer at the time,<br>but the seemingly right way to solve the problem above is to use:<br><br>(datum->syntax #'id (symbol-append* "set-" #'id) #'id) <br>
<br>This creates an identifier which "context" is the same as that of id.<br>It was much simpler than I feared.<br><br>No, I wonder why such getter/setter feature does not exist<br>by default in Racket's class system, since something similar<br>
already exists for structs.<br>Is it because it is considered malpractice, or simply because<br>there has been no real need to implement that, or... ?<br><br><br><br></div></div>