New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/go2go: compiler treats returning type with generics incorrectly. #39655
Comments
It'd be easy to be fixed by |
This is working as expected. This code will work:
Alternatively, you need to write This is a common mistake people are making, so we are considering changing this. This is also documented in the design draft. |
@griesemer Thanks for clarifying. |
@griesemer - Sorry to comment on a closed issue, but perhaps it might make sense to add a line in https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#using-generic-types-as-unnamed-function-parameter-types mentioning that we need to write When I read that section, it seemed to suggest that the new proposal actually breaks backward compatibility, so there's no need to write code with extra parentheses. |
@agnivade I'm not sure quite what you are saying. It's OK to write |
Ah yea sorry about that. I realize now that I went on a slight tangent. I just wanted some clarification about the syntax added to the doc. In my case, what I needed was
This is what I would like to be added in the doc. (Or is it already there somewhere and I have missed it?) |
I think that the current parser doesn't yet fully support the syntax described in the design draft. Let's revisit when that work is done. |
FWITW, the required pair of surrounding parentheses gets removed by Example (https://go2goplay.golang.org/p/lr-Nvr1ydPA) package main
func main() {}
type A(type T) interface{}
// (1) try:
func _(type T)() (A(T), error) { return nil, nil }
//
// --> error: mixed named and unnamed function parameters
// (2) add parentheses:
func _(type T)() ((A(T)), error) { return nil, nil }
//
// --> ok, compiles
// (3) run gofmt on (2) gets you back to (1) Something similar (https://go2goplay.golang.org/p/zJ7wmOIPWxK) package main
func main() {}
type A(type T) interface{}
func _(type T)() A(T) { return nil } // ok
func _(type T)() (A(T)) { return nil } // nok: cannot convert nil (untyped nil value) to T
func _(type T)() ((A(T))) { return nil } // ok
func _(type T)() (((A(T)))) { return nil } // ok
func _(type T)() (A(T), error) { return nil, nil } // mixed named and unnamed function parameters
func _(type T)() ((A(T)), error) { return nil, nil } // ok
func _(type T)() (a A(T), err error) { return nil, nil } // ok
func _(type T)() (a (A(T)), err error) { return nil, nil } // ok PS: Sorry to comment on a closed issue, but I also don't think this is worth opening a new one, just let me know if I should do it anyway. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
No. It's a dev branch.
What operating system and processor architecture are you using (
go env
)?I'm using https://go2goplay.golang.org/.
What did you do?
What did you expect to see?
The code should be compiled
What did you see instead?
The return type of F should be Result(T), but the compiler thinks it's T, treating the definition as
func F(type T)(v Result(T)) (Result T)
.The text was updated successfully, but these errors were encountered: