英文:
Go generics: invalid composite literal type T
问题
以下是您提供的代码的翻译:
package main
import (
"google.golang.org/protobuf/proto"
)
type NetMessage struct {
Data []byte
}
type Route struct {
}
type AbstractParse interface {
Parse(*NetMessage) proto.Message
}
type MessageParse[T proto.Message] struct {
}
func (p *MessageParse[T]) Parse(message *NetMessage) proto.Message {
protoT := &T{}
if len(message.Data) > 0 {
err := proto.Unmarshal(message.Data, protoT)
if err != nil {
return nil
}
}
return protoT
}
当我尝试在Go中使用泛型编码时,遇到了这个问题:
./prog.go:23:13: 无效的复合字面量类型 T
这是什么原因?有没有办法修复它?
代码链接:https://go.dev/play/p/oRiH2AyaYb6
英文:
package main
import (
"google.golang.org/protobuf/proto"
)
type NetMessage struct {
Data []byte
}
type Route struct {
}
type AbstractParse interface {
Parse(*NetMessage) proto.Message
}
type MessageParse[T proto.Message] struct {
}
func (p *MessageParse[T]) Parse(message *NetMessage) proto.Message {
protoT := &T{}
if len(message.Data) > 0 {
err := proto.Unmarshal(message.Data, protoT)
if err != nil {
return nil
}
}
return protoT
}
When I tried generic coding for Go, I encountered this problem:
> ./prog.go:23:13: invalid composite literal type T
What was the cause? Is there any way to fix it?
code link: https://go.dev/play/p/oRiH2AyaYb6
答案1
得分: 3
不确定你是否需要泛型...但让我们解决一下编译错误:
invalid composite literal type T
以及关于复合字面量的Go规范:
> LiteralType的核心类型T必须是结构体、数组、切片或映射类型(除非类型以TypeName的形式给出,语法规定了这个约束)。
存在问题的代码是:
type MessageParse[T proto.Message] struct {}
func (p *MessageParse[T]) Parse(message *NetMessage) proto.Message {
protoT := &T{} // <- 这里
泛型类型T
受限于类型proto.Message
。查看类型proto.Message(它是类型protoreflect.ProtoMessage的别名)可以看到它是一个Go的interface
类型,而不是核心类型。因此,它不能用于实例化复合字面量。
在非泛型的示例中,你会得到相同的编译错误:
type mytype interface {
SomeMethod() error
}
_ = &mytype{} // // ERROR: invalid composite literal type mytype
英文:
Not sure you need Generics... but let's address your compilation error:
invalid composite literal type T
and the Go spec regarding composite literal:
> The LiteralType's core type T must be a struct, array, slice, or map
> type (the grammar enforces this constraint except when the type is
> given as a TypeName).
The code at issues is:
type MessageParse[T proto.Message] struct {}
func (p *MessageParse[T]) Parse(message *NetMessage) proto.Message {
protoT := &T{} // <- here
The generic type T
is constrained on the type proto.Message
. Looking at type proto.Message (which is an alias for type protoreflect.ProtoMessage) shows it is a Go interface
type and NOT a core type. Thus it cannot be used to instantiate a composite literal.
You would get the same compilation error in a non-Generics example like:
type mytype interface {
SomeMethod() error
}
_ = &mytype{} // // ERROR: invalid composite literal type mytype
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论