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

go/types: generic type parameter cannot be inferred from calling function's return type #50398

Closed
andrewortman opened this issue Dec 30, 2021 · 2 comments

Comments

@andrewortman
Copy link

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

$ go version
go version go1.18beta1 linux/amd64

Does this issue reproduce with the latest release?

With the current beta1 release

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=REDACTED
GONOSUMDB=REDACTED
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=REDACTED
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18beta1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=REDACTED
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 -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2326630220=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Reproduction: https://go.dev/play/p/GYUfSmIuzx4?v=gotip

What did you expect to see?

In the sandbox code, the function ReturnsUnsetOptionalInteger should have compiled because the generic parameter V could be inferred from it's explicit return type Optional[int]

What did you see instead?

./prog.go:33:25: cannot infer V (prog.go:19:23)
@andrewortman
Copy link
Author

Full example code:

// You can edit this code!
// Click here and start typing.
package main

import "fmt"

type Optional[V any] struct {
	Value V
	Set   bool
}

func NewSetOptional[V any](val V) Optional[V] {
	return Optional[V]{
		Value: val,
		Set:   true,
	}
}

func NewUnsetOptional[V any]() Optional[V] {
	return Optional[V]{
		Set: false,
	}
}

func ReturnsOptionalInteger() Optional[int] {
	// THIS IS FINE:
	return NewSetOptional(3)
}

func ReturnsUnsetOptionalInteger() Optional[int] {
	// DOES NOT COMPILE:
	// should be able to infer V from function return type
	return NewUnsetOptional()

	// Workaround:
	// return NewUnsetOptional[int]()
}

func main() {
	fmt.Println("%v", ReturnsOptionalInteger())
	fmt.Println("%v", ReturnsUnsetOptionalInteger())
}

@andrewortman andrewortman changed the title generics: generic type parameter cannot be inferred from calling function's return typee generics: generic type parameter cannot be inferred from calling function's return type Dec 30, 2021
@andrewortman andrewortman changed the title generics: generic type parameter cannot be inferred from calling function's return type go/types: generic type parameter cannot be inferred from calling function's return type Dec 30, 2021
@seankhliao
Copy link
Member

Duplicate of #49297

@seankhliao seankhliao marked this as a duplicate of #49297 Dec 30, 2021
@golang golang locked and limited conversation to collaborators Dec 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants