[racket] struct-copy with parent type

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Mon May 27 13:43:43 EDT 2013

This exact code couldn't go in, because struct-copy is written in the
kernel language, so bringing in syntax-parse and match is not allowed.
The definition is about 150 lines because of all the different error
checks and the pain of #%kernel.

Furthermore... this code has some problems like not rebinding all the
fields simultaneously, which struct-copy does.

BUT the idea of a new racket/struct module with improved version of
macros like this would be nice in my mind. In particular, it might be
a nice place for a super-struct like [1] macro that gives you keyword
field names, control over the names of predicates, etc.

What does your struct-open do?

1. https://github.com/jeapostrophe/exp/blob/master/sstruct-tests.rkt


On Fri, May 24, 2013 at 2:40 PM, Matthias Felleisen
<matthias at ccs.neu.edu> wrote:
>
> Jay, should this code go into the struct-copy module? I had to write a struct-open macro recently, and I think I should add it too.
>
>
>
>
> On May 24, 2013, at 3:16 PM, Jay McCarthy wrote:
>
>> On Fri, May 24, 2013 at 11:18 AM, Nick Shelley <nickmshelley at gmail.com> wrote:
>>> The struct-copy docs say "The result of struct-expr can be an instance of a
>>> sub-type of id, but the resulting copy is an immediate instance of id (not
>>> the sub-type)." Why is this?
>>
>> The technical reason this is the case is that in (struct-copy <struct>
>> ... ...) the <struct> binding describes the fields of the structure
>> and gives access to the constructor. Thus, in your example code, you
>> are saying "copy this posn" and not "copy this thing and modify the
>> posn pieces". You could imagine that we could create a link between
>> parent and sub-structures, but it would be messy and imperative, in my
>> mind.
>>
>> Alternatively, you could make a version that lists the possible
>> children directly:
>>
>> https://github.com/jeapostrophe/exp/blob/cf6822f41c7637d9074638d0e9b4d4d2d7d27d7b/struct-copy-ish.rkt
>>
>> Alternatively, you could use generics
>>
>>> For instance, I would hope this would work:
>>>
>>> (struct posn (x y))
>>> (struct 3d-posn posn (z))
>>> (3d-posn-z (struct-copy posn (3d-posn 1 2 3) [x 5]))
>>>
>>> My actual use case is that I'm representing some data with structs. I have
>>> the common data in a parent struct and the specific data in the child
>>> structs. One of the common fields is a unique id (a number I just
>>> increment). I'd like to be able to copy a piece of data and just change the
>>> unique id in the struct-copy. Instead I have to have a cond or a match that
>>> does the same struct-copy but with different struct ids for each sub type.
>>>
>>> Is there an easier way to do what I'm trying to do?
>>>
>>> ____________________
>>>  Racket Users list:
>>>  http://lists.racket-lang.org/users
>>>
>>
>>
>>
>> --
>> Jay McCarthy <jay at cs.byu.edu>
>> Assistant Professor / Brigham Young University
>> http://faculty.cs.byu.edu/~jay
>>
>> "The glory of God is Intelligence" - D&C 93
>> ____________________
>>  Racket Users list:
>>  http://lists.racket-lang.org/users
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93

Posted on the users mailing list.