I have an interesting constraint/invariant and I would love to hear people's input on how to maintain it. Big bonus if it can be ensured at compile time...I have the types below, and the following conceptual invariant... If an attribute's id equals an attributeValue's id, then they must have corresponding types. How would you express this invariant and ensure it is true in code?  (The code is Swift, but the question is generic. For those that don't know Swift, these are all immutable value types. They can be used just like integers or immutable strings in other languages.)struct Attribute {    let attributeID: Int    let name: String    let display: Bool    let type: AttributeType}enum AttributeType {    case Text    case SingleList([AttributeChoice])    case MultiList([AttributeChoice])    case Date    case Time    case DateTime    case Image    case Flag    case Count}struct AttributeValue {    let attributeID: Int    let value: AttributeValueType}enum AttributeValueType {    case Text(String)    case SingleList(AttributeChoice)    case MultiList([AttributeChoice])    case Date(NSDate)    case Time(NSDate)    case DateTime(NSDate)    case Image(UIImage)    case Flag(Bool)    case Count(Int)}struct AttributeChoice {    let serverID: Int    let name: String}As I am writing this question, I was thinking that maybe the Attribute class should manage the creation of AttributeValues somehow, any other ideas?
How to ensure this constraint at compile time?
functional programming;swift language;invariants