[plt-scheme] define-struct/contract with super

From: Jon Rafkind (rafkind at cs.utah.edu)
Date: Fri Sep 4 17:27:37 EDT 2009

Jon Rafkind wrote:
> define-struct/contract doesn't seem to currently support super 
> structures as in.
>
> (define-struct/contract (my-struct super-struct) (...))
>
> For now I used define-struct in a separate module and 
> provide/contract'd the sub-struct, but is it possible to make 
> define-struct/contract handle super-structs? I saw an email from Arjun 
> in 2005 that attempted this.

I spent a little while trying to make define-struct/contract be able to 
handle super-structs but don't quite have a solution yet. I think I do 
understand the issues involved so I wanted to hear what others think 
about the design.

Currently a contract is put on the struct constructor, make-foo, and it 
might be hard to get super-constructor's contracts to add in to the 
sub-struct's constructor. That is if foo is a super-type and bar is a 
subtype then you need something like

(make-bar foo-field1-contract foo-field2-contract ... 
bar-field1-contract bar-field2-contract ...)

When I hacked up define-struct/contract I eventually replaced the 
constructor contract with any/c which then let me create sub-types but 
of course without contracts negating the point.

An alternative seems to be putting the contracts in the guard. If the 
sub-type checks the contracts for its own values and passes along the 
super-types values then everything would work fine. If the super-type 
uses contracts for its own fields then it can check those fields during 
its own guard procedure. If the super-type doesn't use contracts then 
the standard define-struct semantics occur.

I haven't implemented contracts in the guard but is there some reason 
things don't already work this way?


Posted on the users mailing list.