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/compile: Unable to apply type parameters to embed type #51810

Closed
mattn opened this issue Mar 19, 2022 · 3 comments
Closed

cmd/compile: Unable to apply type parameters to embed type #51810

mattn opened this issue Mar 19, 2022 · 3 comments

Comments

@mattn
Copy link
Member

@mattn mattn commented Mar 19, 2022

Compiler can't apply type parameters to embed type in compisite literals.

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

$ go version
go version devel go1.19-9956a5423e Thu Mar 17 03:41:50 2022 +0000 windows/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
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\mattn\AppData\Local\go-build
set GOENV=C:\Users\mattn\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\mattn\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\mattn\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\dev\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\dev\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=devel go1.19-9956a5423e Thu Mar 17 03:41:50 2022 +0000
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\mattn\go\src\github.com\mattn\go-tensorflow-realtime-object-detection\go.mod
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\mattn\AppData\Local\Temp\go-build1850869103=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import (
	"fmt"
)

type Based[B any] struct {
	v1 B
}

type Derived[T, B any] struct {
	Based[B]
	v2 T
}

func main() {
	v := Derived[string, int]{
		Based: Based[int]{
			v1: 1,
		},
		v2: "abc",
	}
	fmt.Println(v.v1, v.v2)
}

https://play.golang.com/p/LXEpnd3fxBm

What did you expect to see?

It is possible to omit [int] for inner literal.

package main

import (
	"fmt"
)

type Based[B any] struct {
	v1 B
}

type Derived[T, B any] struct {
	Based[B]
	v2 T
}

func main() {
	v := Derived[string, int]{
		Based: Based {
			v1: 1,
		},
		v2: "abc",
	}
	fmt.Println(v.v1, v.v2)
}

What did you see instead?

./prog.go:18:10: cannot use generic type Based[B any] without instantiation

https://play.golang.com/p/AwgNEDDi7zT

If compile without type parameters, it pass to compile.

https://play.golang.com/p/8J8FAusLKe2

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 19, 2022

https://go.dev/doc/faq#type_inference

Closing because this is not a bug. There are a number of type inferences that we could add to the language. We're not going to do any of them until we have more experience with generics.

@mattn
Copy link
Member Author

@mattn mattn commented Mar 19, 2022

Thank you. BTW, Is it worth making proposal to name the instance-time type name like below?

func main() {
	v := Derived[string, T int]{
		Based: Based[T] {
			v1: 1,
		},
		v2: "abc",
	}
	fmt.Println(v.v1, v.v2)
}

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 19, 2022

@mattn I don't think we're going to be considering refinements like giving a name to a type argument anytime soon. We need more experience.

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

No branches or pull requests

2 participants