Skip to content

Commit 56c9b51

Browse files
committed
spec: introduce alias declarations and type definitions
To avoid confusion caused by the term "named type" (which now just means a type with a name, but formerly meant a type declared with a non-alias type declaration), a type declaration now comes in two forms: alias declarations and type definitions. Both declare a type name, but type definitions also define new types. Replace the use of "named type" with "defined type" elsewhere in the spec. For #18130. Change-Id: I49f5ddacefce90354eb65ee5fbf10ba737221995 Reviewed-on: https://go-review.googlesource.com/36213 Reviewed-by: Rob Pike <r@golang.org>
1 parent 3b68a64 commit 56c9b51

File tree

1 file changed

+109
-50
lines changed

1 file changed

+109
-50
lines changed

doc/go_spec.html

Lines changed: 109 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of January 31, 2017",
3+
"Subtitle": "Version of February 3, 2017",
44
"Path": "/ref/spec"
55
}-->
66

@@ -685,11 +685,9 @@ <h2 id="Variables">Variables</h2>
685685
<h2 id="Types">Types</h2>
686686

687687
<p>
688-
A type determines the set of values and operations specific to values of that
689-
type. Types may be <i>named</i> or <i>unnamed</i>. Named types are specified
690-
by a (possibly <a href="#Qualified_identifiers">qualified</a>)
691-
<a href="#Type_declarations"><i>type name</i></a>; unnamed types are specified
692-
using a <i>type literal</i>, which composes a new type from existing types.
688+
A type determines a set of values together with operations and methods specific
689+
to those values. A type may be denoted by a <i>type name</i>, if it has one,
690+
or specified using a <i>type literal</i>, which composes a type from existing types.
693691
</p>
694692

695693
<pre class="ebnf">
@@ -702,6 +700,7 @@ <h2 id="Types">Types</h2>
702700
<p>
703701
Named instances of the boolean, numeric, and string types are
704702
<a href="#Predeclared_identifiers">predeclared</a>.
703+
Other named types are introduced with <a href="#Type_declarations">type declarations</a>.
705704
<i>Composite types</i>&mdash;array, struct, pointer, function,
706705
interface, slice, map, and channel types&mdash;may be constructed using
707706
type literals.
@@ -717,16 +716,23 @@ <h2 id="Types">Types</h2>
717716
</p>
718717

719718
<pre>
720-
type T1 string
721-
type T2 T1
722-
type T3 []T1
723-
type T4 T3
719+
type (
720+
A1 = string
721+
A2 = A1
722+
)
723+
724+
type (
725+
B1 string
726+
B2 B1
727+
B3 []B1
728+
B4 B3
729+
)
724730
</pre>
725731

726732
<p>
727-
The underlying type of <code>string</code>, <code>T1</code>, and <code>T2</code>
728-
is <code>string</code>. The underlying type of <code>[]T1</code>, <code>T3</code>,
729-
and <code>T4</code> is <code>[]T1</code>.
733+
The underlying type of <code>string</code>, <code>A1</code>, <code>A2</code>, <code>B1</code>,
734+
and <code>B2</code> is <code>string</code>.
735+
The underlying type of <code>[]B1</code>, <code>B3</code>, and <code>B4</code> is <code>[]B1</code>.
730736
</p>
731737

732738
<h3 id="Method_sets">Method sets</h3>
@@ -1417,11 +1423,10 @@ <h3 id="Type_identity">Type identity</h3>
14171423
</p>
14181424

14191425
<p>
1420-
Two <a href="#Types">named types</a> are identical if their type names originate in the same
1421-
<a href="#Type_declarations">TypeSpec</a>.
1422-
A named and an <a href="#Types">unnamed type</a> are always different. Two unnamed types are identical
1423-
if the corresponding type literals are identical, that is, if they have the same
1424-
literal structure and corresponding components have identical types. In detail:
1426+
A <a href="#Type_definitions">defined type</a> is always different from any other type.
1427+
Otherwise, two types are identical if their <a href="#Types">underlying</a> type literals are
1428+
structurally equivalent; that is, they have the same literal structure and corresponding
1429+
components have identical types. In detail:
14251430
</p>
14261431

14271432
<ul>
@@ -1460,31 +1465,47 @@ <h3 id="Type_identity">Type identity</h3>
14601465

14611466
<pre>
14621467
type (
1463-
T0 []string
1464-
T1 []string
1465-
T2 struct{ a, b int }
1466-
T3 struct{ a, c int }
1467-
T4 func(int, float64) *T0
1468-
T5 func(x int, y float64) *[]string
1468+
A0 = []string
1469+
A1 = A0
1470+
A2 = struct{ a, b int }
1471+
A3 = int
1472+
A4 = func(A3, float64) *A0
1473+
A5 = func(x int, _ float64) *[]string
14691474
)
1475+
1476+
type (
1477+
B0 A0
1478+
B1 []string
1479+
B2 struct{ a, b int }
1480+
B3 struct{ a, c int }
1481+
B4 func(int, float64) *B0
1482+
B5 func(x int, y float64) *A1
1483+
)
1484+
1485+
type C0 = B0
14701486
</pre>
14711487

14721488
<p>
14731489
these types are identical:
14741490
</p>
14751491

14761492
<pre>
1477-
T0 and T0
1493+
A0, A1, and []string
1494+
A2 and struct{ a, b int }
1495+
A3 and int
1496+
A4, func(int, float64) *[]string, and A5
1497+
1498+
B0, B0, and C0
14781499
[]int and []int
14791500
struct{ a, b *T5 } and struct{ a, b *T5 }
1480-
func(x int, y float64) *[]string and func(int, float64) (result *[]string)
1501+
func(x int, y float64) *[]string, func(int, float64) (result *[]string), and A5
14811502
</pre>
14821503

14831504
<p>
1484-
<code>T0</code> and <code>T1</code> are different because they are named types
1485-
with distinct declarations; <code>func(int, float64) *T0</code> and
1486-
<code>func(x int, y float64) *[]string</code> are different because <code>T0</code>
1487-
is different from <code>[]string</code>.
1505+
<code>B0</code> and <code>B1</code> are different because they are new types
1506+
created by distinct <a href="#Type_definitions">type definitions</a>;
1507+
<code>func(int, float64) *B0</code> and <code>func(x int, y float64) *[]string</code>
1508+
are different because <code>B0</code> is different from <code>[]string</code>.
14881509
</p>
14891510

14901511

@@ -1502,7 +1523,7 @@ <h3 id="Assignability">Assignability</h3>
15021523
<li>
15031524
<code>x</code>'s type <code>V</code> and <code>T</code> have identical
15041525
<a href="#Types">underlying types</a> and at least one of <code>V</code>
1505-
or <code>T</code> is not a <a href="#Types">named type</a>.
1526+
or <code>T</code> is not a <a href="#Type_definitions">defined</a> type.
15061527
</li>
15071528
<li>
15081529
<code>T</code> is an interface type and
@@ -1511,7 +1532,7 @@ <h3 id="Assignability">Assignability</h3>
15111532
<li>
15121533
<code>x</code> is a bidirectional channel value, <code>T</code> is a channel type,
15131534
<code>x</code>'s type <code>V</code> and <code>T</code> have identical element types,
1514-
and at least one of <code>V</code> or <code>T</code> is not a named type.
1535+
and at least one of <code>V</code> or <code>T</code> is not a defined type.
15151536
</li>
15161537
<li>
15171538
<code>x</code> is the predeclared identifier <code>nil</code> and <code>T</code>
@@ -1840,23 +1861,60 @@ <h3 id="Iota">Iota</h3>
18401861
<h3 id="Type_declarations">Type declarations</h3>
18411862

18421863
<p>
1843-
A type declaration binds an identifier, the <i>type name</i>, to a new type
1844-
that has the same <a href="#Types">underlying type</a> as an existing type,
1845-
and operations defined for the existing type are also defined for the new type.
1846-
The new type is <a href="#Type_identity">different</a> from the existing type.
1864+
A type declaration binds an identifier, the <i>type name</i>, to a <a href="#Types">type</a>.
1865+
Type declarations come in two forms: Alias declarations and type definitions.
1866+
<p>
1867+
1868+
<pre class="ebnf">
1869+
TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
1870+
TypeSpec = AliasDecl | TypeDef .
1871+
</pre>
1872+
1873+
<h4 id="Alias_declarations">Alias declarations</h4>
1874+
1875+
<p>
1876+
An alias declaration binds an identifier to the given type.
18471877
</p>
18481878

18491879
<pre class="ebnf">
1850-
TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
1851-
TypeSpec = identifier Type .
1880+
AliasDecl = identifier "=" Type .
18521881
</pre>
18531882

1883+
<p>
1884+
Within the <a href="#Declarations_and_scope">scope</a> of
1885+
the identifier, it serves as an <i>alias</i> for the type.
1886+
</p>
1887+
18541888
<pre>
1855-
type IntArray [16]int
1889+
type (
1890+
nodeList = []*Node // nodeList and []*Node are identical types
1891+
Polar = polar // Polar and polar denote identical types
1892+
)
1893+
</pre>
18561894

1895+
1896+
<h4 id="Type_definitions">Type definitions</h4>
1897+
1898+
<p>
1899+
A type definition binds an identifier to a newly created type
1900+
with the same <a href="#Types">underlying type</a> and
1901+
operations as the given type.
1902+
</p>
1903+
1904+
<pre class="ebnf">
1905+
TypeDef = identifier Type .
1906+
</pre>
1907+
1908+
<p>
1909+
The new type is called a <i>defined type</i>.
1910+
It is <a href="#Type_identity">different</a> from any other type,
1911+
including the type it is created from.
1912+
</p>
1913+
1914+
<pre>
18571915
type (
1858-
Point struct{ x, y float64 }
1859-
Polar Point
1916+
Point struct{ x, y float64 } // Point and struct{ x, y float64 } are different types
1917+
polar Point // polar and Point denote different types
18601918
)
18611919

18621920
type TreeNode struct {
@@ -1872,8 +1930,9 @@ <h3 id="Type_declarations">Type declarations</h3>
18721930
</pre>
18731931

18741932
<p>
1875-
The declared type does not inherit any <a href="#Method_declarations">methods</a>
1876-
bound to the existing type, but the <a href="#Method_sets">method set</a>
1933+
A defined type may have <a href="#Method_declarations">methods</a> associated with it.
1934+
It does not inherit any methods bound to the given type,
1935+
but the <a href="#Method_sets">method set</a>
18771936
of an interface type or of elements of a composite type remains unchanged:
18781937
</p>
18791938

@@ -1901,8 +1960,8 @@ <h3 id="Type_declarations">Type declarations</h3>
19011960
</pre>
19021961

19031962
<p>
1904-
A type declaration may be used to define a different boolean, numeric, or string
1905-
type and attach methods to it:
1963+
Type definitions may be used to define different boolean, numeric,
1964+
or string types and associate methods with them:
19061965
</p>
19071966

19081967
<pre>
@@ -1924,8 +1983,8 @@ <h3 id="Type_declarations">Type declarations</h3>
19241983
<h3 id="Variable_declarations">Variable declarations</h3>
19251984

19261985
<p>
1927-
A variable declaration creates one or more variables, binds corresponding
1928-
identifiers to them, and gives each a type and an initial value.
1986+
A variable declaration creates one or more <a href="#Variables">variables</a>,
1987+
binds corresponding identifiers to them, and gives each a type and an initial value.
19291988
</p>
19301989

19311990
<pre class="ebnf">
@@ -2083,8 +2142,8 @@ <h3 id="Method_declarations">Method declarations</h3>
20832142
</p>
20842143

20852144
<pre class="ebnf">
2086-
MethodDecl = "func" Receiver MethodName ( Function | Signature ) .
2087-
Receiver = Parameters .
2145+
MethodDecl = "func" Receiver MethodName ( Function | Signature ) .
2146+
Receiver = Parameters .
20882147
</pre>
20892148

20902149
<p>
@@ -2093,7 +2152,7 @@ <h3 id="Method_declarations">Method declarations</h3>
20932152
Its type must be of the form <code>T</code> or <code>*T</code> (possibly using
20942153
parentheses) where <code>T</code> is a type name. The type denoted by <code>T</code> is called
20952154
the receiver <i>base type</i>; it must not be a pointer or interface type and
2096-
it must be declared in the same package as the method.
2155+
it must be <a href="#Type_definitions">defined</a> in the same package as the method.
20972156
The method is said to be <i>bound</i> to the base type and the method name
20982157
is visible only within <a href="#Selectors">selectors</a> for type <code>T</code>
20992158
or <code>*T</code>.

0 commit comments

Comments
 (0)