[plt-scheme] A macro for declaring class properties, a question
On 10/18/07, Grant Rettke <grettke at acm.org> wrote:
> (define-syntax property
> (lambda (stx)
You don't need to unhygenically introduce an identifier, as all
accesses of the field should go through the accessor method, right?
> (define gen-id ; Source: _TSPL3rdEd_ by Kent Dybvig
> (lambda (template-id . args)
> (datum->syntax-object
> template-id
> (string->symbol
> (apply
> string-append
> (map
> (lambda (x)
> (if (string? x) x
> (symbol->string (syntax-object->datum x))))
> args))))))
> (syntax-case stx ()
> [(_ name)
> (with-syntax
> ([property-name (gen-id (syntax name) "field-" (syntax name))])
> (syntax
> (begin
> (field (property-name null))
Maybe use case-lambda for the accessor, as you have exactly two cases:
no args or one arg (not sure if case-lambda works in the class system)
> (define/public (name . args)
> (if (null? args) property-name
> (set! property-name (car args)))))))])))
Something like this would do:
(define-syntax property
(syntax-rules ()
[(_ name)
(begin (field super-secret-field-name)
(define/public name
(case-lambda
[() super-secret-field-name]
[(value) (set! super-secret-field-name value)])))]))
N.