[plt-scheme] A macro for declaring class properties, a question

From: Grant Rettke (grettke at acm.org)
Date: Thu Oct 18 00:48:52 EDT 2007

Hi folks,

I wanted to start by writing a macro to declare (MzLib) class
properties. A class property feature looks like:

(field (field-X null))
(define/public (X . args)
    (if (null? args) x
        (set! field-X (car args))))

Classes can look like this now:

(define service-call%
  (class* object% ()
    (property customer-name)
    (property customer-id)
    (property call-type-code)
    (property date-of-call-string)
    (super-new)))

I'm only learning about macros. That said, if you have a look at the
macro (at the end), is there anything obviously wrong about it? right
about it? any comments or advice?

The next step is that I would like to be able to do this:

(define service-call%
  (class* object% ()
    (property customer-name customer-id call-type-code date-of-call-string)
    (super-new)))

But I don't understand how to do that. Any ideas?

For reading I am sticking with TSPL3 and the Help Desk. Anywhere else
I should look?

Best wishes,

Grant Rettke

(define-syntax property
  (lambda (stx)
    (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))
            (define/public (name . args)
              (if (null? args) property-name
                  (set! property-name (car args)))))))])))


Posted on the users mailing list.