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/go2go: instantiate types from different packages? #40977

Closed
andig opened this issue Aug 22, 2020 · 4 comments
Closed

cmd/go2go: instantiate types from different packages? #40977

andig opened this issue Aug 22, 2020 · 4 comments

Comments

@andig
Copy link
Contributor

@andig andig commented Aug 22, 2020

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

$ go version
go version devel +12c2128b9e Fri Aug 21 00:12:31 2020 +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=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/andig/Library/Caches/go-build"
GOENV="/Users/andig/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/andig/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/andig/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/andig/htdocs/golang"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/andig/htdocs/golang/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/andig/htdocs/evcc/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/73/89ycv7qn51j4kbm04jsz9b840000gn/T/go-build017716575=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

This may likely not be supported yet, but I'm trying to declare a generic in one package and use it from another:

registry.go2:

package util

import (
	"fmt"
)

type Registry[Elem any] map[string]func(map[string]interface{}) (Elem, error)
...

and

config.go2:

package meter

import (
	"github.com/andig/evcc/api"
	"github.com/andig/evcc/util"
)

var registry util.Registry[api.Meter] = make(map[string]func(map[string]interface{}) (api.Meter, error))

What did you expect to see?

Transpile the consuming side without error.

What did you see instead?

After registry.go2 is transpiled, config.go2 transpiling fails:

❯ go2 tool go2go translate meter/config.go2
meter/config.go2:11:27: expected ';', found '[' (and 1 more errors)

Having everything in a single playground file works fine: https://go2goplay.golang.org/p/OAJT9nCTv0u

Btw: the square brackets syntax seems much easier to read than the repeated rounded brackets used in the examples before. Dropping type seems logical and further reduces cognitive load. Playground2 gofmt still adds the type back in, hope this can be dropped in the future.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 22, 2020

This is confusion as to whether your config.go2 file is using square brackets or parentheses. We'll get this fixed at some point. In the meantime use go tool go2go -brackets.

@andig
Copy link
Contributor Author

@andig andig commented Aug 23, 2020

Feeling really stupid, but still:

❯ go2 tool go2go -brackets translate meter/config.go2           
meter/config.go2:11:27: expected ';', found '[' (and 1 more errors)
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 23, 2020

Are you on the tip of the dev.go2go branch? If you are, show me a complete test case. With the test case I created based on the fragments you showed above, adding -brackets fixed the problem.

@andig
Copy link
Contributor Author

@andig andig commented Aug 24, 2020

Are you on the tip of the dev.go2go branch? If you are, show me a complete test case. With the test case I created based on the fragments you showed above, adding -brackets fixed the problem.

❯ go2 version 
go version devel +12c2128b9e Fri Aug 21 00:12:31 2020 +0000 darwin/amd64

The two files in question are in https://github.com/andig/evcc/pull/307/files. Both combined into a single go playground are fine: https://go2goplay.golang.org/p/y0MoIvA-ZVd. Probably not worth spending more time, I'll wait for the experiment to mature more and hopefully make into mainline soonish.

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.