如何测试泛型类型是否适合更严格的接口 – golang

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

How to test if a generic type can fit a more restrictive interface - golang

问题

我正在构建一个通用的数据结构,我打算在不同的上下文和使用不同的数据位使用它。

我目前正在尝试创建一个匹配器,它将查找我的数据结构并返回包含给定数据的所有节点。我的问题是,由于我希望我的数据结构尽可能通用,我需要我的数据是与any匹配的通用类型,这将不允许我进行相等性比较。

我已经构建了一个“后代类型”(可能有一个正确的术语,我是自学的),它具有更严格的comparable约束。

我想知道是否可以从更通用的类型转换为更具体的类型(即使我必须捕获错误并将其返回给用户)。我知道我不是特别需要这样做,但如果我这样做,代码在后续阶段会更容易理解。

这里有一段代码来解释我的问题:

type DataStructureGeneralCase[T any] struct {
	/*
	   我的数据结构,太长了,不适合作为一个好的示例,所以我使用了一个切片
	*/
	data []T
}

type DataStructureSpecific[T comparable] DataStructureGeneralCase[T]

// 这个可以工作,因为any包含comparable

func (ds *DataStructureSpecific[T]) GetMatching(content T) int {
	/*实际函数返回我的自定义节点类型,但让我们简短一些*/
	for idx, item := range ds.data {
		if item == content {
			return idx
		}
	}
	return -1
}

func (dg *DataStructureGeneralCase[T]) TryMatching(content T) (int, error) {
	if ds, ok := (*dg).(DataStructureGeneralCase); ok {
		// 这不起作用,因为dg不是接口,我理解
	} else {
		return -1, fmt.Errorf("由于非可比较的内容,无法进行转换")
	}
}

我的问题可以总结为“我该如何进行转换?”

英文:

I am building an all-purpose data structure, that I intend to use in various contexts and with various bits of data.

I am currently attempting to make a matcher, that will look into my structure and return all nodes containing the data given. My problem being that, since I need my structure to be as generic as possible, I need my data to be of a generic type matching any, and this won't allow me to make equalities.

I have built a "descendant type" (there's probably a correct term, I'm self-taught on this) that has the more rigorous comparable constraint.

I want to see if I can convert from the more general one to the more specific one (even if I have to catch an error and return it to the user). I know that I don't specifically need to, but it makes the code understandable down the line if i do it like that.

Here's a bit of code to explain my question :

type DataStructureGeneralCase[T any] struct {
	/*
	   my data structure, which is too long to make for a good example, so I'm using a slice instead
	*/
	data []T
}

type DataStructureSpecific[T comparable] DataStructureGeneralCase[T]

// this works because any contains comparable

func (ds *DataStructureSpecific[T]) GetMatching(content T) int {
	/*The real function returns my custom Node type, but let's be brief*/
	for idx, item := range ds.data {
		if item == content {
			return idx
		}
	}
	return -1
}

func (dg *DataStructureGeneralCase[T]) TryMatching(content T) (int, error) {
	if ds, ok := (*dg).(DataStructureGeneral); ok {
		// Does not work because dg is not interface, which I understand
	} else {
		return -1, fmt.Errorf("Could not convert because of non-comparable content")
	}
}

My question can be summarized to "How can I do my conversion ?".

答案1

得分: 3

func CheckImplements[T any, I any]() bool {
	var o T
	_, ok := interface{}(&o).(I)
	return ok
}

所以,只是运行时。

英文:
func CheckImplements[T any, I any]() bool {
	var o T
	_, ok := interface{}(&o).(I)
	return ok
}

So yeah, runtime only.

答案2

得分: 0

首先将其转换为空接口:

castedDg := interface{}(dg)
if ds, ok := castedDg.(DataStructureGeneralCase[T]); ok {

他们在https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#why-not-permit-type-assertions-on-values-whose-type-is-a-type-parameter中解释了为什么选择这种方法。

英文:

Cast to the empty interface first:

castedDg := interface{}(dg)
if ds, ok := castedDg.(DataStructureGeneralCase[T]); ok {

They explain why they chose this approach in https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#why-not-permit-type-assertions-on-values-whose-type-is-a-type-parameter

huangapple
  • 本文由 发表于 2022年8月10日 01:02:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/73295599.html
匿名

发表评论

匿名网友

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

确定