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: unable to implement method of an interface with a generic return value #50828

Closed
teken opened this issue Jan 26, 2022 · 2 comments
Closed
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.
Milestone

Comments

@teken
Copy link

teken commented Jan 26, 2022

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

devel go1.18-6eb58cdffa Tue Jan 25 22:56:45 2022 +0000

Does this issue reproduce with the latest release?

Yes latest 1.18 beta release

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

I am using go.dev/play

What did you do?

Go Playground

TL;DR I am trying to implement a method from an interface that has a generic to define it return type

I was trying to create a SumOf function that allowed for the selecting of a property for summing. I had a Product struct with a Cost property, I also have a SumCostProduct struct, that extends Product, that implements a Selectable interface that uses a Number constraint. This should allow for the implementation of the Selected function from the Selectable interface for SumCostProduct.

What did you expect to see?

I expected to compile and run the program with the return 13

What did you see instead?

./prog.go:26:14: cannot use generic type Selectable[E Number] without instantiation
./prog.go:29:14: v.Selected undefined (type T has no field or method Selected)

Go build failed.
@mknyszek mknyszek changed the title affected/package: Unable to implement function from a interface with a return of a constrained generic go/types: unable to implement method of an interface with a generic return value Jan 26, 2022
@mknyszek mknyszek added 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. labels Jan 26, 2022
@mknyszek mknyszek added this to the Go1.18 milestone Jan 26, 2022
@fzipp
Copy link
Contributor

fzipp commented Jan 26, 2022

The correct function signature is:

func SumOf[T Selectable[TR], TR Number](items []T) TR {

(You forgot a [TR] after Selectable.)

https://go.dev/play/p/t-9qdtJQDI5?v=gotip

@mknyszek
Copy link
Contributor

What you wrote isn't going to quite work because the fact is int never actually gets propagated to TR.

Here's a modified version of the code you wrote that works, and I think maintains the spirit of what you wanted: https://go.dev/play/p/N0ws6b0hov9?v=gotip

There may be a way to make Product non-generic, but I'm not really sure how to keep Selectable generic without it. It almost sounds like in this case Selectable should not be generic.

Anyway, as far as I can tell this is WAI, so not a Go issue. In the future, please ask questions first at https://groups.google.com/g/golang-nuts. Closing...

@golang golang locked and limited conversation to collaborators Jan 26, 2023
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.
Projects
None yet
Development

No branches or pull requests

4 participants