"Cannot use .this" 在方法声明中对多个类型参数使用 _ 时出错
"Cannot use .this" error when using _ for multiple type parameters in method declaration
我正在使用 Go 来研究类型参数(泛型)1.18beta1
。
问题
考虑以下片段:
package main
import (
"fmt"
)
func main() {
foo := &Foo[string, int]{
valueA: "i am a string",
valueB: 123,
}
fmt.Println(foo)
}
type Foo[T1 any, T2 any] struct {
valueA T1
valueB T2
}
func (f *Foo[_,_]) String() string {
return fmt.Sprintf("%v %v", f.valueA, f.valueB)
}
此代码段构建失败并出现以下错误:
<autogenerated>:1: cannot use .this (type *Foo[string,int]) as type *Foo[go.shape.string_0,go.shape.string_0] in argument to (*Foo[go.shape.string_0,go.shape.int_1]).String
我试图在方法声明中使用 _
因为 Type Parameters Proposal 中的以下语句:
The type parameters listed in a method declaration need not have the same names as the type parameters in the type declaration. In particular, if they are not used by the method, they can be _.
问题
构建错误是在 1.18beta1
中的错误之上还是我遗漏了什么?
构建成功的代码段变体
使用类型参数名称
如果我将 String
方法声明更改为以下内容(将 _
替换为实际类型参数),我可以使代码构建成功:
func (f *Foo[T1,T2]) String() string {
return fmt.Sprintf("%v %v", f.valueA, f.valueB)
}
单一类型参数
我成功地使用了 _
当只使用一个类型参数时:
package main
import (
"fmt"
)
func main() {
foo := &Foo[string]{"i am a string"}
fmt.Println(foo)
}
type Foo[T1 any] struct {
value T1
}
func (f *Foo[_]) String() string {
return fmt.Sprintf("%v", f.value)
}
为 T1 和 T2 实例化相同类型的 Foo
_
也适用于方法声明,如果 Foo
是用相同类型实例化的(例如 string
用于 T1
和 T2
):
package main
import (
"fmt"
)
func main() {
foo := &Foo[string, string]{
valueA: "i am a string",
valueB: "i am also a string",
}
fmt.Println(foo)
}
type Foo[T1 any, T2 any] struct {
valueA T1
valueB T2
}
func (f *Foo[_,_]) String() string {
return fmt.Sprintf("%v %v", f.valueA, f.valueB)
}
如问题评论中所述,所描述的行为是 Go 1.18beta1 中的错误,issue 50419 正在跟踪。
编辑
我已确认相关错误已在 2022 年 1 月 31 日发布的 1.18beta2 中修复。
我正在使用 Go 来研究类型参数(泛型)1.18beta1
。
问题
考虑以下片段:
package main
import (
"fmt"
)
func main() {
foo := &Foo[string, int]{
valueA: "i am a string",
valueB: 123,
}
fmt.Println(foo)
}
type Foo[T1 any, T2 any] struct {
valueA T1
valueB T2
}
func (f *Foo[_,_]) String() string {
return fmt.Sprintf("%v %v", f.valueA, f.valueB)
}
此代码段构建失败并出现以下错误:
<autogenerated>:1: cannot use .this (type *Foo[string,int]) as type *Foo[go.shape.string_0,go.shape.string_0] in argument to (*Foo[go.shape.string_0,go.shape.int_1]).String
我试图在方法声明中使用 _
因为 Type Parameters Proposal 中的以下语句:
The type parameters listed in a method declaration need not have the same names as the type parameters in the type declaration. In particular, if they are not used by the method, they can be _.
问题
构建错误是在 1.18beta1
中的错误之上还是我遗漏了什么?
构建成功的代码段变体
使用类型参数名称
如果我将 String
方法声明更改为以下内容(将 _
替换为实际类型参数),我可以使代码构建成功:
func (f *Foo[T1,T2]) String() string {
return fmt.Sprintf("%v %v", f.valueA, f.valueB)
}
单一类型参数
我成功地使用了 _
当只使用一个类型参数时:
package main
import (
"fmt"
)
func main() {
foo := &Foo[string]{"i am a string"}
fmt.Println(foo)
}
type Foo[T1 any] struct {
value T1
}
func (f *Foo[_]) String() string {
return fmt.Sprintf("%v", f.value)
}
为 T1 和 T2 实例化相同类型的 Foo
_
也适用于方法声明,如果 Foo
是用相同类型实例化的(例如 string
用于 T1
和 T2
):
package main
import (
"fmt"
)
func main() {
foo := &Foo[string, string]{
valueA: "i am a string",
valueB: "i am also a string",
}
fmt.Println(foo)
}
type Foo[T1 any, T2 any] struct {
valueA T1
valueB T2
}
func (f *Foo[_,_]) String() string {
return fmt.Sprintf("%v %v", f.valueA, f.valueB)
}
如问题评论中所述,所描述的行为是 Go 1.18beta1 中的错误,issue 50419 正在跟踪。
编辑
我已确认相关错误已在 2022 年 1 月 31 日发布的 1.18beta2 中修复。