[plt-scheme] Setter an class inner function call

From: Noel Welsh (noelwelsh at gmail.com)
Date: Wed Mar 10 06:51:38 EST 2010

On Wed, Mar 10, 2010 at 11:24 AM, Laurent <laurent.orseau at gmail.com> wrote:

> Searching for my way through macros (and seeing some shiny lights ahead), I
> wanted to make a getter/setter helper for class fields.
> Here is the setter I came up with:
>
>> (define-syntax (setter stx)
>>   (syntax-case stx ()
>>     [(_ id)
>>      (with-syntax ([set-id (symbol-append* "set-" #'id)]
>>                    [val (gensym)])
>>        #'(define/public (set-id val) (set! id val))
>>        )]
>>     [(_ id1 id2 ...)
>>      #'(begin (setter id1)
>>               (setter id2 ...))]
>>     ))
>>

You don't need the gensym. Hygiene will take of this automatically.
So, the following works:

(define-syntax (setter stx)
  (syntax-case stx ()
    [(_ id)
     (with-syntax ([set-id (symbol-append* "set-" #'id)])
       #'(define/public (set-id val) (set! id val))
       )]
    [(_ id1 id2 ...)
     #'(begin (setter id1)
              (setter id2 ...))]
    ))

I don't know how to solve your other problem other than by
hygienically introducing the identifier.

N.


Posted on the users mailing list.