[racket] One accessor for the same fields of different structs?

From: Ben Duan (yfefyf at gmail.com)
Date: Mon Nov 25 09:20:26 EST 2013

Hi All,

If I have several structures, like the following:

    (struct customer (name age foo))
    (struct student (name age bar))

I choose the same representation for the `name` and `age` fields which are
`string?` and `natural-number/c`. So I think I could just define a `name`
accessor for both `customer-name` and `student-name` to save some typing. I
have to define the accesors by hand like:

    (define (name record)
      (cond
        ((customer? record) (customer-name record))
        ((student? record) (student-name record))))
    (define (age record)
      (cond
        ((customer? record) (customer-age record))
        ((student? record) (student-age record))))

1. It's much repetitive work. Is there any built-in way to do these things?
    I think macro can do these. But I'm trying to avoid macros as much as I
could, because I don't trust myself in designing macros.

2. Is it good or bad style to use one accessor for different structs?

Thanks,
Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131125/cdbe9627/attachment.html>

Posted on the users mailing list.