[racket] [plt-scheme] Setter an class inner function call

From: Laurent (laurent.orseau at gmail.com)
Date: Thu Sep 2 06:54:33 EDT 2010

(unearthing an old topic to provide an answer)

On Wed, Mar 10, 2010 at 13:51, Noel Welsh <noelwelsh at gmail.com> wrote:

> 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.
>

(Now I've finally learned that all such identifiers are renamed.)


> 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.
>

I probably didn't understand that last bit of your answer at the time,
but the seemingly right way to solve the problem above is to use:

(datum->syntax #'id (symbol-append* "set-" #'id) #'id)

This creates an identifier which "context" is the same as that of id.
It was much simpler than I feared.

No, I wonder why such getter/setter feature does not exist
by default in Racket's class system, since something similar
already exists for structs.
Is it because it is considered malpractice, or simply because
there has been no real need to implement that, or... ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100902/b0ba0525/attachment.html>

Posted on the users mailing list.