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/compile/internal/types2: "interface is (or embeds) comparable" for map keys #47337

Closed
ainar-g opened this issue Jul 22, 2021 · 3 comments
Closed

Comments

@ainar-g
Copy link
Contributor

@ainar-g ainar-g commented Jul 22, 2021

(I'm not really sure if it's a bug or just an extension of the current type parameters mechanism that wasn't proposed or implemented yet, so please feel free to merge into other issues or close as duplicate.)

I've been playing around with the current type params implementation, and found that this code:

type nullableMakeable[T any] interface {
	type
		[]T,
		map[comparable]T
}

func emptyIfNil[T any, C nullableMakeable[T]](v C) (res C) {
	if v == nil {
		return make(C, 0)
	}

	return v
}

func nilIfEmpty[T any, C nullableMakeable[T]](v C) (res C) {
	if v != nil && len(v) == 0 {
		return nil
	}

	return v
}

Doesn't compile:

$ gotip run --gcflags '-G=3' ./go/typar/main.go
  # command-line-arguments
  go/typar/main.go:69:7: interface is (or embeds) comparable

gotip version is go version devel go1.17-3e48c0381f Wed Jul 21 20:29:18 2021 +0000 linux/amd64.

The intention here is to have an interface, nullableMakeable, that is the interface of types that are nullable, have a length, and that can be instantiated with make. I haven't found anything prohibiting this in the draft design/proposal, although it's a rather big document, so I might have missed something.

@zephyrtronium
Copy link
Contributor

@zephyrtronium zephyrtronium commented Jul 22, 2021

map[comparable]T is not valid. comparable is a constraint and not a type. It can only appear in type parameter lists.

Loading

@ainar-g
Copy link
Contributor Author

@ainar-g ainar-g commented Jul 22, 2021

By the way, the proposal currently says that comparable, like all constraints, can be embedded:

Since comparable, like all constraints, is an interface type, it can be embedded in another interface type used as a constraint:

// ComparableHasher is a type constraint that matches all
// comparable types with a Hash method.
type ComparableHasher interface {
	comparable
	Hash() uintptr
}

So at the very least the error message “interface is (or embeds) comparable” is misleading, as it implies that embedding comparable is prohibited.

Loading

@thanm
Copy link
Contributor

@thanm thanm commented Jul 22, 2021

@ainar-g, I am not sure if you will get useful signal from building things with "gotip run --gcflags '-G=3' " -- gotip tracks the master branch, whereas active development of generics has moved (a while back) to the "dev.typeparams" branch.

Would also like to reiterate that we don't use the issue tracker for questions in Go, I think you would be better off bringing up your question about embedding "comparable" on the golang-dev mailing list.

Closing out this issue, please re-open if you disagree with the above. Thanks.

Loading

@thanm thanm closed this Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants