Skip to content

proposal: go/types: add HasTypeName interface #66890

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

Open
adonovan opened this issue Apr 18, 2024 · 5 comments
Open

proposal: go/types: add HasTypeName interface #66890

adonovan opened this issue Apr 18, 2024 · 5 comments
Assignees
Labels
Milestone

Comments

@adonovan
Copy link
Member

During the types.Alias work, there was a recurring need (7 times in x/tools; @findleyr is adding an eighth) for this interface:

package types // import "go/types"

// HasTypeName abstracts the three kinds of types that have declared names:
// aliases ([*Alias]), defined types ([*Named]), and type parameters ([*TypeParam]).
//
// Note that the Go spec considers built-in types such as string and int to
// be defined types, but this package represents them as [*Basic],
// since they do not have a declaration or [TypeName].
type HasTypeName interface {
    Obj() *TypeName
}

Of course users can easily define it for themselves, but adding it to go/types provides a good place to hang additional documentation.

Related:

@findleyr @griesemer

@gopherbot gopherbot added this to the Proposal milestone Apr 18, 2024
@adonovan adonovan moved this to Incoming in Proposals Apr 18, 2024
@adonovan adonovan self-assigned this Apr 18, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/604476 mentions this issue: go/types: clarify Named, Alias, TypeName, Object

gopherbot pushed a commit that referenced this issue Sep 20, 2024
Updates #65855
Updates #66890

Change-Id: I167c9de818049cae02f0d99f8e0fb4017e07bea9
Reviewed-on: https://go-review.googlesource.com/c/go/+/604476
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
@griesemer
Copy link
Contributor

I note that we have an unexported function

// hasName reports whether t has a name. This includes
// predeclared types, defined types, and type parameters.
// hasName may be called with types that are not fully set up.
func hasName(t Type) bool {
	switch Unalias(t).(type) {
	case *Basic, *Named, *TypeParam:
		return true
	}
	return false
}

Per the spec: "Predeclared types, defined types, and type parameters are called named types."

Maybe the interface should just be called "HasName". Having theObj method is not quite the same as being a named type (aliases have Obj but may denote type literals - one of the mistakes we made when introducing aliases).

@adonovan
Copy link
Member Author

In the branch where I was playing with this I ended up using the name TypeWithName to make clear that the interface is a restriction of Type; otherwise it's a bit unclear what kind of thing it is.

@findleyr
Copy link
Member

My 2 cents: types.HasName seems better than types.TypeWithName

@griesemer
Copy link
Contributor

In the spec, a named type has a very specific meaning; specifically, an alias type may not be a named type. This interface is about whether a type has an Obj method which returns a TypeName. I think HasTypeName or HasObj seem ok names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants