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

Generics chapter: example for calling a function with a type that cannot be inferred #770

Open
VillePuuska opened this issue Jun 5, 2024 · 1 comment

Comments

@VillePuuska
Copy link

In the Generics chapter, could we add an example showing how to call a function with a type parameter that cannot be inferred? Specifically, this happens when a function returns a value with a type that cannot be inferred from its parameters. I wondered about this when playing with generics after reading the chapter. Gopls showed me how to do it, but I think adding an example of this to the book could be helpful.

For a concrete example, we could add a constructor for the type Stack[T] introduced in the chapter:

func NewStack[T any]() *Stack[T] {
	return new(Stack[T])
}

which requires the type T to be specified when calling, e.g.:

stringStack := NewStack[string]()
intStack := NewStack[int]()

This example could be added to the end of the section Next: Generic data types.

If this sounds good to you, I'd be happy to write it and make a PR.

@quii
Copy link
Owner

quii commented Jun 21, 2024

Yeah go for it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants