[racket] using struct-copy with struct subtypes?

From: Sam Tobin-Hochstadt (samth at cs.indiana.edu)
Date: Mon Nov 17 12:25:12 EST 2014

Unfortunately, you can't really do this in a general way for structs.

There are a couple possible solutions:
 - use objects/classes and implement a `clone` or a `replace` method
 - use generics, with a `clone` or `replace` generic function


On Mon, Nov 17, 2014 at 12:22 PM, Matthew Butterick <mb at mbtype.com> wrote:
> This seems rudimentary, but I can't figure it out. `struct-copy` requires you to specify the id of the struct type, like so:
> (define copied-struct (struct-copy struct-type-id instance-of-struct))
> The docs for struct-copy say "subtypes can be copied as if they were supertypes, but the result is an instance of the supertype". [1]
> OK, but suppose I want the opposite behavior: I want to make a struct-copying function that accepts instances of a supertype or subtype, but outputs a struct that's the same type as the input instance (not the supertype).
> Possible? Or not possible with structs, because that's what objects & classes are for?
> [1] http://docs.racket-lang.org/reference/struct-copy.html
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.