(unearthing an old topic to provide an answer) On Wed, Mar 10, 2010 at 13:51, Noel Welsh <[email protected]> wrote:
> On Wed, Mar 10, 2010 at 11:24 AM, Laurent <[email protected]> > 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... ?
_________________________________________________ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users

