// structField is a type constraint whose type set consists of some
// struct types that all have a field named x.
type structField interface {
struct { a int; x int } |
struct { b int; x float64 } |
struct { c int; x uint64 }
}
// This function is INVALID.
func IncrementX[T structField](p *T) {
v := p.x // INVALID: type of p.x is not the same for all types in set
v++
p.x = v
}
However, it still fails to compile if all the types of the x fields are identical.
type structField interface {
struct { a int; x int } |
struct { b int; x int } |
struct { c int; x int }
}
func IncrementX[T structField](p *T) {
v := p.x // p.x undefined (type *T has no field or method x)
v++
p.x = v // p.x undefined (type *T has no field or method x)
}