[plt-scheme] Setter an class inner function call
Hi all,
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 ...))]
> ))
>
>
I hope I've done things right, otherwise please tell me.
It uses the two below functions:
(define-for-syntax (symbol-append* . args)
> (string->symbol
> (apply string-append (map ->string args))))
>
>
(define-for-syntax (->string x)
> (cond [(syntax? x) (->string (syntax->datum x))]
> [else (format "~a" x)]
> ))
>
The setter can be used in a class:
(define a%
> (class object% (super-new)
> (init-field x)
> (setter x)
> ))
>
There is one problem though:
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.
My question: What should I modify the setter macro do so that the setter
function (like set-x) could be bound at expand-phase?
Strangely, I have no problem with the getter, because it's seems like the
method get-something is already defined for any field.
Thanks,
Laurent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100310/0ba0d2d7/attachment.html>