Mar*_*ois 19 generics field go
我想让下面的代码编译。通过阅读类型参数提案(Go Generics),我的理解是这应该可行,但我一定错过了一些东西。
package main
import "fmt"
func main() {
s := Struct{A: "Hello World!"}
PrintA(s)
}
func PrintA[T Type](v T) {
fmt.Printf("%s\n", v.A)
}
type Type interface {
struct{ A string }
}
type Struct struct {
A string
}
func (s Struct) String() string {
return s.A
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
./prog.go:7:8:Struct 未实现 Type(约束 Type 中的 struct{A string} 可能缺少 ~)
./prog.go:11:23:vA 未定义(类型 T 没有字段或方法 A )
我想T用特定类型的特定字段来表示所有结构。添加~没有帮助。
以下是已实施的提案中的一个示例,它是最新 Go beta 版本的一部分。
type structField interface {
struct { a int; x int } |
struct { b int; x float64 } |
struct { c int; x uint64 }
}
Run Code Online (Sandbox Code Playgroud)
bla*_*een 30
Go 1.18 已禁用字段访问(Go 1.19 中仍禁用)。Go 1.18 发行说明提到了这一点:
当前的泛型实现具有以下已知限制:
[...]
- Go 编译器不支持访问类型为参数类型的结构体字段
x.f,x即使类型参数的类型集中的所有类型都有一个字段f。我们可能会在 Go 1.19 中删除此限制。
任何结构类型的解决方法都归结为旧的无聊的基于接口的多态性:
type Type interface {
GetA() string
}
func (s Struct) GetA() string {
return s.A
}
Run Code Online (Sandbox Code Playgroud)
此时您甚至不必使用Type接口作为约束。它可以只是一个普通的接口类型:
func PrintA(v Type) {
fmt.Printf("%s\n", v.GetA())
}
Run Code Online (Sandbox Code Playgroud)
如果您同意仅将此接口用作约束,则可以添加类型元素来限制哪些结构可以实现它:
type Type interface {
StructFoo | StructBar
GetA() string
}
Run Code Online (Sandbox Code Playgroud)
如果使用指针接收器声明方法,请使用指针类型。
旧答案(不再相关,仅提供信息)
在 2022 年初的某个时候,当此功能仍在开发中时,如果您添加以下内容,您的示例确实可以工作~:
type Type interface {
~struct{ A string }
}
Run Code Online (Sandbox Code Playgroud)
但它只适用于完全定义为的结构struct{ A string },除此之外没有任何作用。定义一个约束“表示具有特定类型的特定字段的所有结构”始终不受支持。有关详细信息,请参阅此答案。
相反,您从提案中引用的示例是关于访问类型集中的公共字段的。通过定义结构体的联合:
type structField interface {
~struct { a int; x int } | ~struct { a int; x float64 }
}
Run Code Online (Sandbox Code Playgroud)
您应该能够访问a此类类型参数的字段,但正如答案开头所述,这又没有实现。如果联合中的所有术语具有相同的基础类型,它曾经可以工作(示例改编自问题#48522)。
自 2022 年 3 月起,此代码不再编译:
package main
import "fmt"
type Point struct {
X, Y int
}
type Rect struct {
X, Y int
}
func GetX[P Point | Rect] (p P) int {
return p.X
}
func main() {
p := Point{1, 2}
r := Rect{2, 3}
fmt.Println("X: %d %d", GetX(p), GetX(r)) // prints X: 1 2
}
Run Code Online (Sandbox Code Playgroud)