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

WBOY
Release: 2024-02-09 15:06:09
forward
912 people have browsed it

为什么 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,
    }
}
Copy after login

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.
Copy after login

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]
Copy after login

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] {...}
Copy after login

Or, if the function is not used at all

t:

func returnConstrainedGeneric() MyStruct[int] {...}
Copy after login

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!

Related labels:
source:stackoverflow.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template