<div dir="ltr"><div class="gmail_extra">No need for a macro here, you simply need to define a common parent struct `person', and derive `customer' and `student' from this struct:<br><br>(struct person (name age))<br>
(struct customer person (foo))<br>(struct student person (bar))<br><br>(person-name (customer "Joe" 34 'foo)) ; -> "Joe"<br><br></div><div class="gmail_extra">The parent struct name must be given right after the struct name of the child.<br>
</div><div class="gmail_extra"><br></div><div class="gmail_extra">Laurent<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 25, 2013 at 3:20 PM, Ben Duan <span dir="ltr"><<a href="mailto:yfefyf@gmail.com" target="_blank">yfefyf@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi All,</div><div><br></div><div>If I have several structures, like the following:</div>
<div><br></div><div> (struct customer (name age foo))</div><div> (struct student (name age bar))</div><div><br>
</div><div>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:</div>
<div><br></div><div> (define (name record)</div><div> (cond</div><div> ((customer? record) (customer-name record))</div><div> ((student? record) (student-name record))))</div><div> (define (age record)</div>
<div> (cond</div><div> ((customer? record) (customer-age record))</div><div> ((student? record) (student-age record))))</div><div><br></div><div>1. It's much repetitive work. Is there any built-in way to do these things?</div>
<div> 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. </div><div><br></div><div>2. Is it good or bad style to use one accessor for different structs?</div>
<div><br></div><div>Thanks,</div><div>Ben</div><div><br></div></div>
<br>____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br></blockquote></div><br></div></div>