Skip to content

type parameter: some methods with specific types can't be compiled if return types contains the specific type #49984

Closed
@smallnest

Description

@smallnest

What version of Go are you using (go version)?

$ go version
go version devel go1.18-3396878af4 Sat Dec 4 17:16:07 2021 +0000 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/smallnest/Library/Caches/go-build"
GOENV="/Users/smallnest/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/smallnest/go/pkg/mod"
GONOPROXY=""
GONOSUMDB="*"
GOOS="darwin"
GOPATH="/Users/smallnest/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/Users/smallnest/sdk/gotip"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/smallnest/sdk/gotip/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="devel go1.18-3396878af4 Sat Dec 4 17:16:07 2021 +0000"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/qj/vk5g8vfs09s94dg375c1db700000l2/T/go-build1693723073=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I have defined a List[T any] type and defined a generic method Len. I have also defined some methods for specific types.
Some methods with specific types can be compiled but some can't:

package type_parameter

type List[T any] struct {
	next  *List[T]
	value T
}

func (l *List[T]) Len() int {
	return 0
}

func (l *List[string]) Length() int {
	return 0
}

// Wrong!!!
// func (l *List[int]) Size() int {
// 	return 0
// }

func (l *List[int]) Say() string {
	return "hello List[int]"
}

// Wrong!!!
// func (l *List[string]) Greet() string {
// 	return "hello List[string]"
// }

What did you expect to see?

All methods with specific types can't be defined or all methods with specific types can be defined. It should keep consistent.

What did you see instead?

If types in return of methods with specific types contains the specific type, the method can't be compiled, for example,(l *List[int]) Size() int and (l *List[string]) Greet() string can'r be compiled :

func (l *List[int]) Size() int {
	return 0
}
func (l *List[string]) Greet() string {
	return "hello List[string]"
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions