<div dir="ltr">Thanks Jay.<div><br></div><div>So with the generics approach I would just create a gen-struct-copy with the same signature as struct-copy, then the implementation of gen-struct-copy in each of my structs would just call struct-copy with the correct struct-id. Is that the right approach?</div>
<div><br></div><div>Also, is there a way to introspect the struct-id if the struct is transparent? Something like:</div><div><br></div><div style>(define p (3posn 1 2 3))</div><div style>(struct-copy (struct-id p) p [x #:parent posn 5])</div>
<div style><br></div><div style>This obviously wouldn't work if p was a posn, but in my use case, the parent class is abstract so I know I'm getting child classes. I scanned through some of the struct docs, but couldn't find a way to introspect a struct type.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, May 24, 2013 at 1:16 PM, Jay McCarthy <span dir="ltr"><<a href="mailto:jay.mccarthy@gmail.com" target="_blank">jay.mccarthy@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Fri, May 24, 2013 at 11:18 AM, Nick Shelley <<a href="mailto:nickmshelley@gmail.com">nickmshelley@gmail.com</a>> wrote:<br>
> The struct-copy docs say "The result of struct-expr can be an instance of a<br>
> sub-type of id, but the resulting copy is an immediate instance of id (not<br>
> the sub-type)." Why is this?<br>
<br>
</div>The technical reason this is the case is that in (struct-copy <struct><br>
... ...) the <struct> binding describes the fields of the structure<br>
and gives access to the constructor. Thus, in your example code, you<br>
are saying "copy this posn" and not "copy this thing and modify the<br>
posn pieces". You could imagine that we could create a link between<br>
parent and sub-structures, but it would be messy and imperative, in my<br>
mind.<br>
<br>
Alternatively, you could make a version that lists the possible<br>
children directly:<br>
<br>
<a href="https://github.com/jeapostrophe/exp/blob/cf6822f41c7637d9074638d0e9b4d4d2d7d27d7b/struct-copy-ish.rkt" target="_blank">https://github.com/jeapostrophe/exp/blob/cf6822f41c7637d9074638d0e9b4d4d2d7d27d7b/struct-copy-ish.rkt</a><br>
<br>
Alternatively, you could use generics<br>
<div class="im"><br>
> For instance, I would hope this would work:<br>
><br>
> (struct posn (x y))<br>
> (struct 3d-posn posn (z))<br>
> (3d-posn-z (struct-copy posn (3d-posn 1 2 3) [x 5]))<br>
><br>
> My actual use case is that I'm representing some data with structs. I have<br>
> the common data in a parent struct and the specific data in the child<br>
> structs. One of the common fields is a unique id (a number I just<br>
> increment). I'd like to be able to copy a piece of data and just change the<br>
> unique id in the struct-copy. Instead I have to have a cond or a match that<br>
> does the same struct-copy but with different struct ids for each sub type.<br>
><br>
> Is there an easier way to do what I'm trying to do?<br>
><br>
</div>> ____________________<br>
> Racket Users list:<br>
> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
<br>
<br>
<br>
--<br>
Jay McCarthy <<a href="mailto:jay@cs.byu.edu">jay@cs.byu.edu</a>><br>
Assistant Professor / Brigham Young University<br>
<a href="http://faculty.cs.byu.edu/~jay" target="_blank">http://faculty.cs.byu.edu/~jay</a><br>
<br>
"The glory of God is Intelligence" - D&C 93<br>
</blockquote></div><br></div>