无法推断 V:无法从约束实现中推断类型参数。

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

cannot infer V: infer type parameter from constraint implementation

问题

我有一个在go中的接口,希望支持在不同的数据库中保存和加载结果,并且我想支持不同的类型。

我为这个接口实现了一个文件系统存储,代码如下:

  1. type FileSystemStorage[K, V WritableType] struct {
  2. }
  3. func (f FileSystemStorage[K, V]) get(key K) (V, error) {
  4. // 从json文件中加载数据的代码
  5. }
  6. func (f FileSystemStorage[K, V]) set(key K, value V) (bool, error) {
  7. // 将数据保存为json文件的代码
  8. }

顺便说一下,当我尝试从fileSystem获取一个实例并调用SetValue时,它可以工作,但是对于GetValue,我遇到了一个编译错误,我的测试代码如下:

  1. var fileStorage cfgStorage.FileSystemStorage[string, string]
  2. setResult, _ := cfgStorage.SetValue(fileStorage, "key", "value")
  3. if setResult == false {
  4. t.Error()
  5. }
  6. var result string
  7. result, _ = cfgStorage.GetValue(fileStorage, "key")

编译错误出现在调用GetValue的那一行:

无法推断出V的类型

如果你有任何解决这个问题的想法,请告诉我!

英文:

I have an interface in go which wants to support saving and loading results in different databases and I want to support different types.

  1. package cfgStorage
  2. type WritableType interface {
  3. ~int | ~string | ~float64
  4. }
  5. type ConfigStorage[K, V WritableType] interface {
  6. get(key K) (V, error)
  7. set(key K, value V) (bool, error)
  8. }
  9. func GetValue[K, V WritableType, C ConfigStorage[K, V]](storage C, key K) (V, error) {
  10. res, err := storage.get(key)
  11. return res, err
  12. }
  13. func SetValue[K, V WritableType, C ConfigStorage[K, V]](storage C, key K, value V) (bool, error) {
  14. res, err := storage.set(key, value)
  15. return res, err
  16. }

I implemented fileSystem storage for this interface as below:

  1. type FileSystemStorage[K, V WritableType] struct {
  2. }
  3. func (f FileSystemStorage[K, V]) get(key K) (V, error) {
  4. /// my code to load data from json file
  5. }
  6. func (f FileSystemStorage[K, V]) set(key K, value V) (bool, error) {
  7. /// my code to save data as json file
  8. }

BTW when I try to get an instance from fileSystem and SetValue it works, but for GetValue I faced a compiler error, my test code is as following:

  1. var fileStorage cfgStorage.FileSystemStorage[string, string]
  2. setResult, _ := cfgStorage.SetValue(fileStorage, "key", "value")
  3. if setResult == false {
  4. t.Error()
  5. }
  6. var result string
  7. result, _ = cfgStorage.GetValue(fileStorage, "key")

The compile error is in the line where I called GetValue:

> cannot infer V

If you have any idea how to solve this issue please let me know!

答案1

得分: 7

在函数GetValue中,只有提供的参数storage Ckey K是无法推断出V的类型的。

你正在要求从实现了泛型约束ConfigStorage[K, V]的具体类型中推断出V。当前的类型推断算法不支持这样做。Go语言的GitHub存储库中有相关的问题,包括41176: cannot infer generic interface types,以及5048440018

还有关于类型推断的相关提案部分:

> 我们可以使用函数参数类型推断来从非类型参数的类型中推断出类型参数。我们可以使用约束类型推断来从已知类型参数中推断出未知类型参数。

因此,你可以认为C实际上并不是已知的,你只知道它实现了约束ConfigStorage[K, V]

你必须使用显式的类型参数调用GetValue

  1. // 第一个string是K的类型,第二个string是V的类型
  2. GetValue[string, string](fileStorage, "key")

修复后的示例代码:https://gotipplay.golang.org/p/KoYZ3JMEz2N

英文:

In the function GetValue, it's not possible to infer the type of V with only the provided arguments storage C and key K.

You are asking to infer V from the concrete type that implements the generic constraint ConfigStorage[K, V]. The current type inference algorithm doesn't support this. Related issues in the Go github repository are 41176: cannot infer generic interface types , as well as 50484 and 40018.

Also relevant proposal section about type inference:

> We can use function argument type inference for a function call to deduce type arguments from the types of the non-type arguments. We can use constraint type inference to deduce unknown type arguments from known type arguments.

So you could argue that C is not actually known, you only know that it implements the constraint ConfigStorage[K, V].

You must call GetValue with explicit type parameters:

  1. // first string for K, second string for V
  2. GetValue[string, string](fileStorage, "key")

Fixed playground: https://gotipplay.golang.org/p/KoYZ3JMEz2N

huangapple
  • 本文由 发表于 2022年1月26日 22:40:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/70865232.html
匿名

发表评论

匿名网友

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

确定