mirror of
https://github.com/golang/go
synced 2024-09-24 11:20:20 -06:00
[dev.typealias] spec: use term 'embedded field' rather than 'anonymous field'
First steps towards defining type aliases in the spec. This is a nomenclature clarification, not a language change. The spec used all three terms 'embedded type', 'anonymous field', and 'embedded field'. Users where using the terms inconsistently. The notion of an 'anonymous' field was always misleading since they always had a de-facto name. With type aliases that name becomes even more important because we may have different names for the same type. Use the term 'embedded field' consistently and remove competing terminology. For #18130. Change-Id: I2083bbc85788cab0b2e2cb1ff58b2f979491f001 Reviewed-on: https://go-review.googlesource.com/35108 Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
9ecc3ee252
commit
f8b4123613
@ -1,6 +1,6 @@
|
|||||||
<!--{
|
<!--{
|
||||||
"Title": "The Go Programming Language Specification",
|
"Title": "The Go Programming Language Specification",
|
||||||
"Subtitle": "Version of November 18, 2016",
|
"Subtitle": "Version of January 31, 2017",
|
||||||
"Path": "/ref/spec"
|
"Path": "/ref/spec"
|
||||||
}-->
|
}-->
|
||||||
|
|
||||||
@ -738,7 +738,7 @@ The method set of any other type <code>T</code> consists of all
|
|||||||
The method set of the corresponding <a href="#Pointer_types">pointer type</a> <code>*T</code>
|
The method set of the corresponding <a href="#Pointer_types">pointer type</a> <code>*T</code>
|
||||||
is the set of all methods declared with receiver <code>*T</code> or <code>T</code>
|
is the set of all methods declared with receiver <code>*T</code> or <code>T</code>
|
||||||
(that is, it also contains the method set of <code>T</code>).
|
(that is, it also contains the method set of <code>T</code>).
|
||||||
Further rules apply to structs containing anonymous fields, as described
|
Further rules apply to structs containing embedded fields, as described
|
||||||
in the section on <a href="#Struct_types">struct types</a>.
|
in the section on <a href="#Struct_types">struct types</a>.
|
||||||
Any other type has an empty method set.
|
Any other type has an empty method set.
|
||||||
In a method set, each method must have a
|
In a method set, each method must have a
|
||||||
@ -947,15 +947,15 @@ Moreover, the inner slices must be initialized individually.
|
|||||||
<p>
|
<p>
|
||||||
A struct is a sequence of named elements, called fields, each of which has a
|
A struct is a sequence of named elements, called fields, each of which has a
|
||||||
name and a type. Field names may be specified explicitly (IdentifierList) or
|
name and a type. Field names may be specified explicitly (IdentifierList) or
|
||||||
implicitly (AnonymousField).
|
implicitly (EmbeddedField).
|
||||||
Within a struct, non-<a href="#Blank_identifier">blank</a> field names must
|
Within a struct, non-<a href="#Blank_identifier">blank</a> field names must
|
||||||
be <a href="#Uniqueness_of_identifiers">unique</a>.
|
be <a href="#Uniqueness_of_identifiers">unique</a>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre class="ebnf">
|
<pre class="ebnf">
|
||||||
StructType = "struct" "{" { FieldDecl ";" } "}" .
|
StructType = "struct" "{" { FieldDecl ";" } "}" .
|
||||||
FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
|
FieldDecl = (IdentifierList Type | EmbeddedField) [ Tag ] .
|
||||||
AnonymousField = [ "*" ] TypeName .
|
EmbeddedField = [ "*" ] TypeName .
|
||||||
Tag = string_lit .
|
Tag = string_lit .
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@ -974,16 +974,15 @@ struct {
|
|||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
A field declared with a type but no explicit field name is an <i>anonymous field</i>,
|
A field declared with a type but no explicit field name is called an <i>embedded field</i>.
|
||||||
also called an <i>embedded</i> field or an embedding of the type in the struct.
|
An embedded field must be specified as
|
||||||
An embedded type must be specified as
|
|
||||||
a type name <code>T</code> or as a pointer to a non-interface type name <code>*T</code>,
|
a type name <code>T</code> or as a pointer to a non-interface type name <code>*T</code>,
|
||||||
and <code>T</code> itself may not be
|
and <code>T</code> itself may not be
|
||||||
a pointer type. The unqualified type name acts as the field name.
|
a pointer type. The unqualified type name acts as the field name.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
|
// A struct with four embedded fields of types T1, *T2, P.T3 and *P.T4
|
||||||
struct {
|
struct {
|
||||||
T1 // field name is T1
|
T1 // field name is T1
|
||||||
*T2 // field name is T2
|
*T2 // field name is T2
|
||||||
@ -1000,15 +999,15 @@ in a struct type:
|
|||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
struct {
|
struct {
|
||||||
T // conflicts with anonymous field *T and *P.T
|
T // conflicts with embedded field *T and *P.T
|
||||||
*T // conflicts with anonymous field T and *P.T
|
*T // conflicts with embedded field T and *P.T
|
||||||
*P.T // conflicts with anonymous field T and *T
|
*P.T // conflicts with embedded field T and *T
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
A field or <a href="#Method_declarations">method</a> <code>f</code> of an
|
A field or <a href="#Method_declarations">method</a> <code>f</code> of an
|
||||||
anonymous field in a struct <code>x</code> is called <i>promoted</i> if
|
embedded field in a struct <code>x</code> is called <i>promoted</i> if
|
||||||
<code>x.f</code> is a legal <a href="#Selectors">selector</a> that denotes
|
<code>x.f</code> is a legal <a href="#Selectors">selector</a> that denotes
|
||||||
that field or method <code>f</code>.
|
that field or method <code>f</code>.
|
||||||
</p>
|
</p>
|
||||||
@ -1025,7 +1024,7 @@ promoted methods are included in the method set of the struct as follows:
|
|||||||
</p>
|
</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
If <code>S</code> contains an anonymous field <code>T</code>,
|
If <code>S</code> contains an embedded field <code>T</code>,
|
||||||
the <a href="#Method_sets">method sets</a> of <code>S</code>
|
the <a href="#Method_sets">method sets</a> of <code>S</code>
|
||||||
and <code>*S</code> both include promoted methods with receiver
|
and <code>*S</code> both include promoted methods with receiver
|
||||||
<code>T</code>. The method set of <code>*S</code> also
|
<code>T</code>. The method set of <code>*S</code> also
|
||||||
@ -1033,7 +1032,7 @@ promoted methods are included in the method set of the struct as follows:
|
|||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
If <code>S</code> contains an anonymous field <code>*T</code>,
|
If <code>S</code> contains an embedded field <code>*T</code>,
|
||||||
the method sets of <code>S</code> and <code>*S</code> both
|
the method sets of <code>S</code> and <code>*S</code> both
|
||||||
include promoted methods with receiver <code>T</code> or
|
include promoted methods with receiver <code>T</code> or
|
||||||
<code>*T</code>.
|
<code>*T</code>.
|
||||||
@ -1434,8 +1433,8 @@ literal structure and corresponding components have identical types. In detail:
|
|||||||
<li>Two struct types are identical if they have the same sequence of fields,
|
<li>Two struct types are identical if they have the same sequence of fields,
|
||||||
and if corresponding fields have the same names, and identical types,
|
and if corresponding fields have the same names, and identical types,
|
||||||
and identical tags.
|
and identical tags.
|
||||||
Two anonymous fields are considered to have the same name. Lower-case field
|
<a href="#Exported_identifiers">Non-exported</a> field names from different
|
||||||
names from different packages are always different.</li>
|
packages are always different.</li>
|
||||||
|
|
||||||
<li>Two pointer types are identical if they have identical base types.</li>
|
<li>Two pointer types are identical if they have identical base types.</li>
|
||||||
|
|
||||||
@ -1445,8 +1444,9 @@ literal structure and corresponding components have identical types. In detail:
|
|||||||
Parameter and result names are not required to match.</li>
|
Parameter and result names are not required to match.</li>
|
||||||
|
|
||||||
<li>Two interface types are identical if they have the same set of methods
|
<li>Two interface types are identical if they have the same set of methods
|
||||||
with the same names and identical function types. Lower-case method names from
|
with the same names and identical function types.
|
||||||
different packages are always different. The order of the methods is irrelevant.</li>
|
<a href="#Exported_identifiers">Non-exported</a> method names from different
|
||||||
|
packages are always different. The order of the methods is irrelevant.</li>
|
||||||
|
|
||||||
<li>Two map types are identical if they have identical key and value types.</li>
|
<li>Two map types are identical if they have identical key and value types.</li>
|
||||||
|
|
||||||
@ -1891,7 +1891,7 @@ type NewMutex Mutex
|
|||||||
type PtrMutex *Mutex
|
type PtrMutex *Mutex
|
||||||
|
|
||||||
// The method set of *PrintableMutex contains the methods
|
// The method set of *PrintableMutex contains the methods
|
||||||
// Lock and Unlock bound to its anonymous field Mutex.
|
// Lock and Unlock bound to its embedded field Mutex.
|
||||||
type PrintableMutex struct {
|
type PrintableMutex struct {
|
||||||
Mutex
|
Mutex
|
||||||
}
|
}
|
||||||
@ -2492,13 +2492,13 @@ If <code>x</code> is a package name, see the section on
|
|||||||
A selector <code>f</code> may denote a field or method <code>f</code> of
|
A selector <code>f</code> may denote a field or method <code>f</code> of
|
||||||
a type <code>T</code>, or it may refer
|
a type <code>T</code>, or it may refer
|
||||||
to a field or method <code>f</code> of a nested
|
to a field or method <code>f</code> of a nested
|
||||||
<a href="#Struct_types">anonymous field</a> of <code>T</code>.
|
<a href="#Struct_types">embedded field</a> of <code>T</code>.
|
||||||
The number of anonymous fields traversed
|
The number of embedded fields traversed
|
||||||
to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
|
to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
|
||||||
The depth of a field or method <code>f</code>
|
The depth of a field or method <code>f</code>
|
||||||
declared in <code>T</code> is zero.
|
declared in <code>T</code> is zero.
|
||||||
The depth of a field or method <code>f</code> declared in
|
The depth of a field or method <code>f</code> declared in
|
||||||
an anonymous field <code>A</code> in <code>T</code> is the
|
an embedded field <code>A</code> in <code>T</code> is the
|
||||||
depth of <code>f</code> in <code>A</code> plus one.
|
depth of <code>f</code> in <code>A</code> plus one.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user