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

spec: Type inference and functional options? #47868

Closed
alecthomas opened this issue Aug 21, 2021 · 3 comments
Closed

spec: Type inference and functional options? #47868

alecthomas opened this issue Aug 21, 2021 · 3 comments
Labels
FrozenDueToAge generics Issue is related to generics NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. TypeInference Issue is related to generic type inference
Milestone

Comments

@alecthomas
Copy link

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

$ go version
go version devel go1.18-e17439e087 Sat Aug 21 00:55:22 2021 +0000 darwin/arm64

Does this issue reproduce with the latest release?

Yes

What did you do?

package main

import "fmt"

type Option[T any] func(*Parser[T])

func Upgrade[T any]() Option[T] {
	return func(*Parser[T]) {}
}

func Description[T any](string) Option[T] {
	return func(*Parser[T]) {}
}

type Parser[T any] struct {}

func New[T any](options...Option[T]) *Parser[T] {
	p := &Parser[T]{}
	for _, o := range options {
		o(p)
	}
	return p
}

type SomeThing struct {}

func main() {
	p := New[SomeThing](Upgrade(), Description("A description"))
	// p := New[SomeThing](Upgrade[SomeThing](), Description[SomeThing]("A description"))
	fmt.Println("???", p)
}

This program fails with the following:

./main.go:24:29: cannot infer T (/Users/aat/Projects/goroot/foo/main.go:7:14) ([<nil>])

Which makes sense given the scope of type inference in the design. However, I thought I'd file this issue anyway, as functional options are very common in Go and this will make them quite tedious to use in conjunction with generic types:

	p := New[SomeThing](Upgrade[SomeThing](), Description[SomeThing]("A description"))

The issue is phrased as a question in case there is some solution I'm not aware of, or it's just plain out of scope.

PS. Thanks for all the hard work getting it to this stage, it's looking super promising :)

@ianlancetaylor
Copy link
Contributor

I think this is mentioned at https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#type-inference-for-generic-function-arguments .

@toothrot toothrot changed the title Type inference and functional options? spec: Type inference and functional options? Aug 24, 2021
@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 24, 2021
@toothrot toothrot modified the milestones: Unreleased, Backlog, Unplanned Aug 24, 2021
@ianlancetaylor ianlancetaylor added the generics Issue is related to generics label Nov 15, 2021
@vikstrous2
Copy link

I worked around this limitation by introducing a config struct that the functional options act on. See vikstrous/dataloadgen@a1d6c74#diff-f5110f13acbf40c384f0191d1967d0333e9d9a533def66f5e8b7518fdc80e226R10 for an example.

@alecthomas
Copy link
Author

I'm okay to close this - this is a specific example of a more general problem described by the proposal and there are workarounds.

@ianlancetaylor ianlancetaylor added the TypeInference Issue is related to generic type inference label Jan 17, 2023
@golang golang locked and limited conversation to collaborators Jan 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge generics Issue is related to generics NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. TypeInference Issue is related to generic type inference
Projects
None yet
Development

No branches or pull requests

5 participants