golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.93k stars 17.65k forks source link

proposal: go/types: add HasTypeName interface #66890

Open adonovan opened 6 months ago

adonovan commented 6 months ago

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 commented 2 months ago

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