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
li-go opened this issue Jun 30, 2020 · 3 comments
Closed

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

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

Comments

@li-go
Copy link

@li-go li-go 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

@li-go
Copy link
Author

@li-go li-go 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

@tdakkota tdakkota commented Jun 30, 2020

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

return make([](Item(T)), size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.