如何动态创建一个结构体并将其用作泛型函数中的类型参数

huangapple go评论73阅读模式
英文:

How to dynamically create a struct and use it as type parameter in a generic function

问题

这是一个可用的示例供您参考:

package main

type (
	builder    struct{}
	ISomething interface {
		DoSomething()
	}
	IMyStruct interface {
		MySomething()
	}
	myStruct struct {
		Num       int
		Something ISomething
	}
	Something struct{}
)

func AddSingleton[T any](b *builder, ctor any) {}

func (m *myStruct) MySomething()  {}
func (s *Something) DoSomething() {}

func main() {
	var something ISomething = &Something{}
	b := &builder{}
	for i := 0; i < 2; i++ {
		AddMyStruct(b, something, i)
	}

}
func AddMyStruct(b *builder, something ISomething, num int) {
	AddSingleton[*myStruct](b, func(something ISomething) *myStruct {
		return &myStruct{Num: num, Something: something}
	})
	AddSingleton[IMyStruct](b, func(obj *myStruct) IMyStruct {
		return obj
	})
}

我正在使用的 DI 库期望在这个片段中传递的 "obj" 是一个唯一的类型。它在第一步中被添加到 DI 中,然后当有人想要使用 IMyStruct 获取相同的实例时,它只解析最后一个添加的该类型。

在我的循环中,有两个被添加,它总是解析最后一个。

我可以要求容器返回一个 []IMyStruct 数组,它确实会返回两个实例,但它们都是最后添加的 *myStruct。即 objs[0].Num == 1objs[1].Num == 1。它应该是 0,1。

我可以使用泛型来添加唯一性:

type myStruct[T any] struct {
	Num       int
	Something ISomething
}

但是当我想要在循环中添加这些对象时,它不起作用。

我在想,如果我动态创建类型,它是原始 myStruct 的完美克隆,那么添加到 DI 中的每个对象都是唯一的类型。因此,在解析时不会出现问题,因为"只能有一个"。

我希望有人能够使用反射技巧,而不是复制 AddMyStruct

英文:

Here is a working example to start with:

package main

type (
	builder    struct{}
	ISomething interface {
		DoSomething()
	}
	IMyStruct interface {
		MySomething()
	}
	myStruct struct {
		Num       int
		Something ISomething
	}
	Something struct{}
)

func AddSingleton[T any](b *builder, ctor any) {}

func (m *myStruct) MySomething()  {}
func (s *Something) DoSomething() {}

func main() {
	var something ISomething = &amp;Something{}
	b := &amp;builder{}
	for i := 0; i &lt; 2; i++ {
		AddMyStruct(b, something, i)
	}

}
func AddMyStruct(b *builder, something ISomething, num int) {
	AddSingleton[*myStruct](b, func(something ISomething) *myStruct {
		return &amp;myStruct{Num: num, Something: something}
	})
	AddSingleton[IMyStruct](b, func(obj *myStruct) IMyStruct {
		return obj
	})
}

The DI library I am using expects that the "obj" being passed in this snippet;

AddSingleton[IMyStruct](b, func(obj *myStruct) IMyStruct {
		return obj
	})

is a unique type. It gets added to the DI in the first step and then when someone wants to get that same instance using IMyStruct it only resolves the last of that type added.
In my for loop, 2 get added and it always resolves the last one.
I can ask the container for an array of []IMyStruct, which does give me 2 back, but they are the last *myStruct added. i.e. the objs[0].Num == 1, objs[1].Num == 1. It should be 0,1.

I can add uniqueness using generics;

type myStruct[T any] struct {
		Num       int
		Something ISomething
	}

but that doesn't work when I want to add these objects in a for loop.

I was thinking that if I dynamically create the type, being a perfect clone of the original myStruct, then everything that gets added to the di is a unique type. Hence no problem resolving it "there can only be one".

I am hoping someone has reflection chops that I don't have to duplicate the AddMyStruct.

答案1

得分: 6

如何动态创建一个结构体,然后将其用作泛型的类型参数?

你无法直接这样做。

(但我必须承认,我不理解你的代码。)

英文:

> How to dynamical create a struct and then use it as a type paramater to a generic

You simply cannot do that.

(But I have to admit I do not understand your code.)

huangapple
  • 本文由 发表于 2023年3月31日 00:03:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75890555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定