Home  >  Article  >  Backend Development  >  Why can't Go functions return types with constrained type parameters?

Why can't Go functions return types with constrained type parameters?

WBOY
WBOYforward
2024-02-09 15:06:09866browse

为什么 Go 函数不能返回带有约束类型参数的类型?

Why can't Go functions return types with constrained type parameters? This is a question that often causes confusion. In the Go language, the return type of a function cannot be a type with constrained type parameters. This is mainly due to the limited support for generics in the Go language. In the Go language, there is no generics mechanism similar to that in Java or C#, and there is no syntax to support constrained type parameters. Therefore, the return type of a function can only be a specific type, and constrained type parameters cannot be used. This means that we cannot define a return type in a function whose parameter type is a constrained type. Such restrictions may make writing code in certain scenarios a little more cumbersome, but they are also part of the Go language design.

Question content

While trying to enforce valid state transitions at compile time in go, I ran into the limitation that functions cannot return generic types with non-concrete type parameters, like so issues stated here. Unable to build mre (go playground link):

type mystruct[t any] struct {
    myfield t
}

func returnconstrainedgeneric[t any]() mystruct[t] {
    return mystruct[int]{
        myfield: 1,
    }
}

Compiler returns error cannot use mystruct[int]{…} (value of type mystruct[int]) as mystruct[t] value in return statements.

The linked question gives this reasoning:

The error occurs because operations that involve a type parameter (including assignments and returns) must be valid for all types in its type set.

It outlines several workarounds including type assertions, but I'm curious why this limitation exists. Naively, in my example I would expect that returning a value of type mystruct[int] from returnconstrainedgeneric() would be valid because int satisfies ## Type constraints for #any. I want the caller of returnconstrainedgeneric() not to know that the return value is of type mystruct[int], it only knows that it is mystruct[t], where t satisfies any constraints. What's missing in my reasoning? Is this a fundamental problem with how go implements generics/type constraints, or is it a problem with the current implementation of the go compiler, or is it something else?

Workaround

This is invalid because the syntax means

x:=returnconstrainedgeneric[string]()
// x is mystruct[string]

But you are trying to return

mystruct[int].

The flaw in your reasoning is that you specify the return type of the function when you instantiate it. This function cannot return a type that satisfies the

any constraint, it returns the instantiated type. In other words, the t of the instantiated function must be the same as the t in mystruct[t].

If you always return

mystruct[int], declare it like this:

func returnconstrainedgeneric[t any]() mystruct[int] {...}

Or, if the function is not used at all

t:

func returnConstrainedGeneric() MyStruct[int] {...}

The above is the detailed content of Why can't Go functions return types with constrained type parameters?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete