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

proposal: go/types: adopt "defined type" terminology consistently #65855

Open
adonovan opened this issue Feb 21, 2024 · 2 comments
Open

proposal: go/types: adopt "defined type" terminology consistently #65855

adonovan opened this issue Feb 21, 2024 · 2 comments
Assignees
Labels
Milestone

Comments

@adonovan
Copy link
Member

The spec uses the term "defined type" for a type created by a declaration such as type T int; a defined type is capable of bearing declared methods. Historically, this was called a Named type, but the advent of type aliases made this confusing and ambiguous.

Though I was initially lukewarm about the term "defined type" (aren't all types defined?), recent work auditing dense type-checking code in x/tools as made me appreciate it, because there are now three kinds of "named" types:

  • defined types (*Named)
  • type parameters (*TypeParam, go1.18), and
  • type aliases (*Alias in go1.22).

In some cases, their namedness is a meaningful commonality that can be used by tooling. For example, they all have an associated TypeName symbol that provides their location and logical name for use in a tool's UI.

I propose that we follow through with the renaming, using the name "Named" as little as possible. Obviously we can't get rid of Named, or make it an alias for Defined (as its reflect.Type.String would change), but we can make Defined an alias for it:

// A Defined type is a type created by a declaration such as `type T underlying`,
// capable of bearing method declarations.
//
// Along with TypeParam and Alias, it is one of three types that are named
// and have an associated TypeName symbol.
// Named is a historical and deprecated term for Defined.
type Defined = types.Named
func NewDefined(obj *TypeName, underlying Type, methods []*Func) *Defined

// Named is the historical term for a Defined type.
// Use Defined in new code.
type Named struct { ... }

// Deprecated: use NewDefined.
func NewNamed( ... ) { return NewDefined ... }
@gopherbot gopherbot added this to the Proposal milestone Feb 21, 2024
@findleyr
Copy link
Contributor

Interesting. I think we've long considered "Named" to have been a misnomer, but not considered changing it. FWIW, I'm not sure that compatibility prohibits Named from being an alias for Defined. Also, predeclared basic types are also named (https://go.dev/ref/spec#Types).

I guess the question is whether this is worth trying to change at this point, or whether it is simpler to continue to live with Named. I think there are many things we would change in the go/types API if we could, and such a superficial change may not be worth the churn.

@go101
Copy link

go101 commented Feb 23, 2024

It looks the latest spec has almost cleared the confusions. Named types include predeclared types, defined types (including ordinary and generic ones), and type parameters. An instantiated type is an ordinary defined type (so also a named type). An alias to an unnamed composite type is not a named type.

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

5 participants