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: better error message for invalid use of constraint interface #42881

Open
voldyman opened this issue Nov 30, 2020 · 5 comments
Open

cmd/go2go: better error message for invalid use of constraint interface #42881

voldyman opened this issue Nov 30, 2020 · 5 comments
Assignees
Milestone

Comments

@voldyman
Copy link

@voldyman voldyman commented Nov 30, 2020

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

fmt.Printf("%s\n", runtime.Version())
devel +9daca74ad2 Wed Nov 18 00:18:01 2020 +0000

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

go2 playground (https://go2goplay.golang.org/)

go env Output
fmt.Printf("%s/%s\n", runtime.GOOS, runtime.GOARCH)
linux/amd64

What did you do?

type SortableComparable interface {
   comparable
   GreaterOrEqual(other SortableComparable) bool
}

https://go2goplay.golang.org/p/LGpaCLkA31a

What did you expect to see?

Successful compilation, since it's the same as the following code which compiles


type sortable interface {
	GreaterOrEqual(other sortable) bool
}

type SortableComparable interface {
	comparable
	sortable
}

https://go2goplay.golang.org/p/f2Lpuv-3JKl

What did you see instead?

type checking failed for main
prog.go2:10:23: interface is (or embeds) comparable
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 30, 2020

This isn't permitted, because it would require the type to have a method GreaterOrEqual(other SortableComparable) bool. But since SortableComparable embeds comparable, it may only be used as a type constraint, not as an ordinary type. So no type can ever have such a method.

But the error message could be better. CC @griesemer.

@griesemer
Copy link
Contributor

@griesemer griesemer commented Nov 30, 2020

As an aside, the two programs you (@voldyman) are comparing are not the same: The first one has a different interface type for the other parameter than the second one.

@griesemer griesemer changed the title cmd/go2go: interface with comparable constraint and self reference fails to compile cmd/go2go: better error message for invalid use of constraint interface Nov 30, 2020
@griesemer griesemer self-assigned this Nov 30, 2020
@voldyman
Copy link
Author

@voldyman voldyman commented Nov 30, 2020

Got it, in that case the error message while trying to create a implementation is also not clear.

https://go2goplay.golang.org/p/-knY7KsfF67

type sortable interface {
	GreaterOrEqual(other sortable) bool
}

type SortableComparable interface {
	comparable
	sortable
}

type Num struct {
	num int
}

func (n Num) GreaterOrEqual(s sortable) bool {
	other, ok := s.(Num)
	if !ok {
		return false
	}
	return n.num >= other.num
}

var _ SortableComparable = Num{2}

type checking failed for main
prog.go2:29:7: interface is (or embeds) comparable

related question: Is there a way to express "the type should have methods a & b, and also be allowed in the key of a map " with the current generics proposal?
(https://go2goplay.golang.org/p/MbM5zhGwMS9)

@griesemer
Copy link
Contributor

@griesemer griesemer commented Dec 2, 2020

@voldyman Interfaces that contain type lists or embed comparable cannot be used as type of a variable (incl. parameter, result, field, etc.); it can only be used as a constraint. But map keys can be interfaces even if they don't embed comparable (that's always been the case). Taking your example and modifying it a bit shows how a SortableComparable can be used as a map key; though there is no way to define your own equality function that then would be used for the map. The latter would require a custom (generic) map, but that will be possible with generics.

@voldyman
Copy link
Author

@voldyman voldyman commented Dec 2, 2020

Thanks for the detailed answer!

(I'll leave this issue open for the compiler error message but feel free to close it if not required)

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
3 participants