Closed susugagalala closed 4 years ago
@susugagalala what you are asking for is function overloading which go does not support. There can only be one declaration of a function (or method) irrespective of the arguments.
I agree with @davecheney . This could arguably be considered for future versions of the language. It will not be part of the initial generics language change proposal (if we ever get to that point).
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, with the above go2go branch and commit date
What operating system and processor architecture are you using (
go env
)?darwin amd64
What did you do?
$go tool go2go run program.go2 // program.go2 package main
func Print(s []int) { for _, v := range s { println(v) } }
func Print[T](s []T) { for _, v := range s { println(v) } }
func main() { Print([]int{1, 2, 3}) }
What did you expect to see?
1 2 3 NOTE: I'd like to see the compiler use non-generic version if both generic/concrete functions exist. That also applies to generic types as well. This also mean existing client code doesn't have to change when generics finally come to Go, so Go2 generics is still effectively backward compatible with Go1.
What did you see instead?
type checking failed for main /var/folders/v9/jczv1kzj41329fqmmsp9m1c40000gn/T/go2go-run303878027/program.go2:9:6: Print redeclared in this block /var/folders/v9/jczv1kzj41329fqmmsp9m1c40000gn/T/go2go-run303878027/program.go2:3:6: other declaration of Print