Home > Backend Development > Golang > How to Resolve Go's 'Cannot Infer Type Parameter V' Error in Generic Functions?

How to Resolve Go's 'Cannot Infer Type Parameter V' Error in Generic Functions?

Linda Hamilton
Release: 2024-12-31 09:26:10
Original
229 people have browsed it

How to Resolve Go's

Cannot Infer Type Parameter V

Consider the following Go code:

package cfgStorage

type WritableType interface {
    ~int | ~string | ~float64
}

type ConfigStorage[K, V WritableType] interface {
    get(key K) (V, error)
    set(key K, value V) (bool, error)
}

func GetValue[K, V WritableType, C ConfigStorage[K, V]](storage C, key K) (V, error) {
    res, err := storage.get(key)
    return res, err
}

func SetValue[K, V WritableType, C ConfigStorage[K, V]](storage C, key K, value V) (bool, error) {
    res, err := storage.set(key, value)
    return res, err
}

type FileSystemStorage[K, V WritableType] struct {
}

func (f FileSystemStorage[K, V]) get(key K) (V, error) {
    /// my code to load data from json file
}

func (f FileSystemStorage[K, V]) set(key K, value V) (bool, error) {
/// my code to save data as json file
}

func main() {
    var fileStorage cfgStorage.FileSystemStorage[string, string]

    setResult, _ := cfgStorage.SetValue(fileStorage, "key", "value")
    if setResult == false {
        log.Fatal("Error setting value")
    }
    var result string

    result, _ = cfgStorage.GetValue(fileStorage, "key")
    fmt.Println(result)
}
Copy after login

When invoking the GetValue function, the Go compiler reports the error:

cannot infer V
Copy after login

Cause

In Go 1.20 and earlier, the type inference algorithm cannot deduce the type of V based solely on the provided arguments storage and key. The constraint type inference rules allow for deducing unknown type arguments from known type arguments. However, in this case, the concrete type that satisfies the ConfigStorage[K, V] constraint is not known.

Solution

To resolve this issue, explicit type parameters must be provided when calling GetValue:

result, _ = GetValue[string, string](fileStorage, "key")
Copy after login

Go 1.21

In Go 1.21, the type inference algorithm has been enhanced to consider methods when a value is assigned to an interface. This means that type parameters used in method signatures can now be inferred from the corresponding parameter types of matching methods. As a result, in Go 1.21 and later, you can simply call:

result, _ = GetValue(fileStorage, "key")
Copy after login

without specifying the type parameters explicitly.

The above is the detailed content of How to Resolve Go's 'Cannot Infer Type Parameter V' Error in Generic Functions?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template