Skip to content
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: making a slice of generics struct doesn't compile #39936

Closed
sltc-li opened this issue Jun 30, 2020 · 3 comments
Closed

cmd/go2go: making a slice of generics struct doesn't compile #39936

sltc-li opened this issue Jun 30, 2020 · 3 comments

Comments

@sltc-li
Copy link

sltc-li commented Jun 30, 2020

What did you do?

https://go2goplay.golang.org/p/ZBpMABeQ0r8

package main

import (
	"fmt"
)

type Item(type T) struct {
	Val T
}

func NewItems(type T)(size int) []Item(T) {
	// Not compile: cannot use generic type Item(type T) without instantiation
	return make([]Item(T), size)
}

func main() {
	items := NewItems(int)(2)
	fmt.Printf("items = %#v\n", items)
}

What did you expect to see?

Making a slice of generics struct compiles

What did you see instead?

type checking failed

@sltc-li
Copy link
Author

sltc-li commented Jun 30, 2020

If I pre-define a generics type of the slice, it compiles.
https://go2goplay.golang.org/p/Wun2YoiGVcg

package main

import (
	"fmt"
)

type Item(type T) struct {
	Val T
}

type Items(type T) []Item(T)

func NewItems(type T)(size int) []Item(T) {
	return make(Items(T), size)
}

func main() {
	items := NewItems(int)(2)
	fmt.Printf("items = %#v\n", items)
}

@tdakkota
Copy link

If you add parentheses, it compiles too.
https://go2goplay.golang.org/p/OSGzqlKrMsM

return make([](Item(T)), size)

@ianlancetaylor
Copy link
Contributor

This is another instance of the ambiguity described at https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#instantiating-types-in-type-literals .

@golang golang locked and limited conversation to collaborators Jun 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants