I'm trying to create a struct with a contract that involves a relationship between two fields. The struct needs to be exported. It seems like what I need is an "invariant contract" as in struct/dc: 8.1 Data-structure Contracts
However I don't see how to export a struct using a contract created through struct/dc. The examples of exporting structs use contract-out and its struct form. But that only allows for structs to have per-field contracts, not a multi-field invariant contract.
Does anyone know how to create and export a struct with a contract that involves multiple fields?
AFAICT, the contract you get from struct/dc is like a stronger version of the instance? predicate you get from the struct form. So you can use it in other contract combinators (like the argument or result positions of ->), and as the contract for a value [my-instance my-struct/dc-contract].
If you wanted to contract the fields and constructor, it's probably a little more boilerplate.
And of course that means you can't use instance in match and struct-copy for all the usual reasons. I'll let someone else post the workaround for that?
Thanks, this makes sense. Yes I see that I can create a instance/c contract with struct/dc, export that, write separate contracts for the constructor and each other procedure, and involve the instance/c in those procedures' contracts. It's a little tedious but works.
It would be nice to be able to export the instance structure type, so that, like you said, features like match can work in other modules. I'm eager to hear if anyone knows how to do that.