We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
go version
$ go version go version devel go1.18-7983830 Tue Oct 5 14:12:44 2021 +0000 linux/amd64
Yes, on latest master.
go env
$ go env
package main type vector[T any] []T func (iv *vector[int]) PushInt(value int) { panic("*(vector[int]).PushInt") } func main() { vs := vector[string]{} vs.PushInt("42") }
I have expected that a primitive type like int won't be allowed as a type parameter. If it's not a compiler error, then PushInt method shall not available for any other instance than vector[int]. I did not find it described in the design document https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md
int
PushInt
vector[int]
It sounds that [int] is treated as [T any], so PushInt is called for vector[string] too.
[int]
[T any]
vector[string]
panic: *(vector[int]).PushInt goroutine 1 [running]: main.(*vector[...]).PushInt(...) /tmp/main.go:7 main.main() /tmp/main.go:12 +0x27 exit status 2
The text was updated successfully, but these errors were encountered:
See #47419 (comment).
Sorry, something went wrong.
@tdakkota thanks! This is not obvious - hope that there will be some linter for it.
linter
To summarize it
func foo[T]() { } // illegal without constraint func (s someStruct[T]) foo() {} // legal, because constraint is defined on a struct level
No branches or pull requests
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, on latest master.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
I have expected that a primitive type like
int
won't be allowed as a type parameter. If it's not a compiler error, thenPushInt
method shall not available for any other instance thanvector[int]
. I did not find it described in the design document https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.mdWhat did you see instead?
It sounds that
[int]
is treated as[T any]
, soPushInt
is called forvector[string]
too.The text was updated successfully, but these errors were encountered: