-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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
Comments
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 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 |
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. |
Change https://go.dev/cl/604476 mentions this issue: |
As an aside, in the past, we did consider introducing type Defined = Named but we also concluded that it would only add more confusion (I'm pretty sure @findleyr and I discussed this). Maybe that's not the case anymore, now that "named types" are essentially types with names (but for alias types that denote type literals). On the other hand, as you say, |
Perhaps that we could even go a bit further. It's not really true for interfaces, which behave structurally. Perhaps that it could even be reconciliated with aliases? Only non-interface type definitions would be able to define a name for a type. A type name is almost a unique identifier for that type, at least within a compilation unit, if we don't take into account types defined within function scopes. Obviously, interface types are identified differently, disregarding where they are defined. A type thus being a "kind" of interface. Just thinking. |
@atdiar A defined interface type (a "nominal interface type") still behaves differently from an interface literal (a "structural interface"): two otherwise identical signatures are different types if one of them contains a defined interface type and the other contains an interface literal (link). In other words, "nominal" is in fact exactly the right word because the name does matter wrt to type identity. Even non-interface types allow assignment of a value of structural type to a variable of nominal type if the underlying types are identical. There's a (different) proposal that would make that difference go away for interfaces, but that's somewhat orthogonal. What you are suggesting is interesting but I am not sure we can make this change in a backward-compatible way. Rather than say that a type definition of an interface produces one of many aliases, I think it would be cleaner to say that the RHS of a type definition cannot be an interface. One can only define alias types for interfaces. |
Oh, you're right. That wouldn't be backward compatible because type assertions/switches. That would require the version-based migration process. Perhaps is it semi-nominal only still? Because variable assignability seem to not mind about the interface name? https://go.dev/play/p/GYnd66YcmWz The composite non interface types seem to be strict about the type definition of each of their arguments indeed. Interesting. (that's not a huge issue, it doesn't make the code wrong, just pondering) |
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>
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 aNamed
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:
*Named
)*TypeParam
, go1.18), and*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:The text was updated successfully, but these errors were encountered: