Field patterns



Either-or Fields

Use disjunctions and an embedding when you want one or another field, but not both.

either-or.cue

#First: {
	foo: string
}

#Other: {
	msg: string
}

#Either: #First | #Other

#Thing: {
	name: string
	#Either
}

thing1: {
	name: "thing1"
	foo:  "bar"
}

thing2: {
	name: "thing2"
	msg:  "hello"
}

cue eval either-or.cue

#First: {
    foo: string
}
#Other: {
    msg: string
}
#Either: {
    foo: string
} | {
    msg: string
}
#Thing: {
    name: string
    foo:  string
} | {
    name: string
    msg:  string
}
thing1: {
    name: "thing1"
    foo:  "bar"
}
thing2: {
    name: "thing2"
    msg:  "hello"
}

2021 Hofstadter, Inc