1
1
<!--{
2
2
"Title": "The Go Programming Language Specification - Go 1.18 Draft",
3
- "Subtitle": "Version of March 7 , 2022",
3
+ "Subtitle": "Version of March 9 , 2022",
4
4
"Path": "/ref/spec"
5
5
}-->
6
6
@@ -761,7 +761,7 @@ <h2 id="Variables">Variables</h2>
761
761
< code > new</ code > call or composite literal, or the type of
762
762
an element of a structured variable.
763
763
Variables of interface type also have a distinct < i > dynamic type</ i > ,
764
- which is the concrete type of the value assigned to the variable at run time
764
+ which is the (non-interface) type of the value assigned to the variable at run time
765
765
(unless the value is the predeclared identifier < code > nil</ code > ,
766
766
which has no type).
767
767
The dynamic type may vary during execution but values stored in interface
@@ -1799,70 +1799,6 @@ <h3 id="Core_types">Core types</h3>
1799
1799
interface{ <-chan int | chan<- int } // directional channels have different directions
1800
1800
</ pre >
1801
1801
1802
- < h3 id ="Specific_types "> Specific types</ h3 >
1803
-
1804
- < p > < b >
1805
- [The definition of specific types is not quite correct yet.]
1806
- </ b > </ p >
1807
-
1808
- < p >
1809
- An interface specification that contains < a href ="#Interface_types "> type elements</ a >
1810
- defines a (possibly empty) set of < i > specific types</ i > .
1811
- Loosely speaking, these are the types < code > T</ code > that appear in the
1812
- interface definition in terms of the form < code > T</ code > , < code > ~T</ code > ,
1813
- or in unions of such terms.
1814
- </ p >
1815
-
1816
- < p >
1817
- More precisely, for a given interface, the set of specific types corresponds to
1818
- the set 𝑅 of representative types of the interface, if 𝑅 is non-empty and finite.
1819
- Otherwise, if 𝑅 is empty or infinite, the interface has < i > no specific types</ i > .
1820
- </ p >
1821
-
1822
- < p >
1823
- For a given interface, type element or type term, the set 𝑅 of representative types is defined as follows:
1824
- </ p >
1825
-
1826
- < ul >
1827
- < li > For an interface with no type elements, 𝑅 is the (infinite) set of all types.
1828
- </ li >
1829
-
1830
- < li > For an interface with type elements,
1831
- 𝑅 is the intersection of the representative types of its type elements.
1832
- </ li >
1833
-
1834
- < li > For a non-interface type term < code > T</ code > or a term of the form < code > ~T</ code > ,
1835
- 𝑅 is the set consisting of the type < code > T</ code > .
1836
- </ li >
1837
-
1838
- < li > For a < i > union</ i > of terms
1839
- < code > t< sub > 1</ sub > |t< sub > 2</ sub > |…|t< sub > n</ sub > </ code > ,
1840
- 𝑅 is the union of the representative types of the terms.
1841
- </ li >
1842
- </ ul >
1843
-
1844
- < p >
1845
- An interface may have specific types even if its < a href ="#Interface_types "> type set</ a >
1846
- is empty.
1847
- </ p >
1848
-
1849
- < p >
1850
- Examples of interfaces with their specific types:
1851
- </ p >
1852
-
1853
- < pre >
1854
- interface{} // no specific types
1855
- interface{ int } // int
1856
- interface{ ~string } // string
1857
- interface{ int|~string } // int, string
1858
- interface{ Celsius|Kelvin } // Celsius, Kelvin
1859
- interface{ float64|any } // no specific types (union is all types)
1860
- interface{ int; m() } // int (but type set is empty because int has no method m)
1861
- interface{ ~int; m() } // int (but type set is infinite because many integer types have a method m)
1862
- interface{ int; any } // int
1863
- interface{ int; string } // no specific types (intersection is empty)
1864
- </ pre >
1865
-
1866
1802
< h3 id ="Type_identity "> Type identity</ h3 >
1867
1803
1868
1804
< p >
@@ -2002,25 +1938,24 @@ <h3 id="Assignability">Assignability</h3>
2002
1938
</ ul >
2003
1939
2004
1940
< p >
2005
- Additionally, if < code > x</ code > 's type < code > V</ code > or < code > T</ code > are type parameters
2006
- with < a href ="#Specific_types "> specific types</ a > , < code > x</ code >
1941
+ Additionally, if < code > x</ code > 's type < code > V</ code > or < code > T</ code > are type parameters, < code > x</ code >
2007
1942
is assignable to a variable of type < code > T</ code > if one of the following conditions applies:
2008
1943
</ p >
2009
1944
2010
1945
< ul >
2011
1946
< li >
2012
1947
< code > x</ code > is the predeclared identifier < code > nil</ code > , < code > T</ code > is
2013
- a type parameter, and < code > x</ code > is assignable to each specific type of
2014
- < code > T</ code > .
1948
+ a type parameter, and < code > x</ code > is assignable to each type in
1949
+ < code > T</ code > 's type set .
2015
1950
</ li >
2016
1951
< li >
2017
1952
< code > V</ code > is not a < a href ="#Types "> named type</ a > , < code > T</ code > is
2018
- a type parameter, and < code > x</ code > is assignable to each specific type of
2019
- < code > T</ code > .
1953
+ a type parameter, and < code > x</ code > is assignable to each type in
1954
+ < code > T</ code > 's type set .
2020
1955
</ li >
2021
1956
< li >
2022
1957
< code > V</ code > is a type parameter and < code > T</ code > is not a named type,
2023
- and values of each specific type of < code > V</ code > are assignable
1958
+ and values of each type in < code > V</ code > 's type set are assignable
2024
1959
to < code > T</ code > .
2025
1960
</ li >
2026
1961
</ ul >
@@ -2055,9 +1990,9 @@ <h3 id="Representability">Representability</h3>
2055
1990
</ ul >
2056
1991
2057
1992
< p >
2058
- If < code > T</ code > is a type parameter with < a href =" #Specific_types " > specific types </ a > ,
1993
+ If < code > T</ code > is a type parameter,
2059
1994
< code > x</ code > is representable by a value of type < code > T</ code > if < code > x</ code > is representable
2060
- by a value of each specific type of < code > T</ code > .
1995
+ by a value of each type in < code > T</ code > 's type set .
2061
1996
</ p >
2062
1997
2063
1998
< pre >
@@ -2705,7 +2640,7 @@ <h4 id="Type_constraints">Type constraints</h4>
2705
2640
< p >
2706
2641
The < a href ="#Predeclared_identifiers "> predeclared</ a >
2707
2642
< a href ="#Interface_types "> interface type</ a > < code > comparable</ code >
2708
- denotes the set of all concrete ( non-interface) types that are
2643
+ denotes the set of all non-interface types that are
2709
2644
< a href ="#Comparison_operators "> comparable</ a > . Specifically,
2710
2645
a type < code > T</ code > implements < code > comparable</ code > if:
2711
2646
</ p >
@@ -3037,6 +2972,14 @@ <h3 id="Operands">Operands</h3>
3037
2972
operand only on the left-hand side of an < a href ="#Assignments "> assignment</ a > .
3038
2973
</ p >
3039
2974
2975
+ < p >
2976
+ Implementation restriction: A compiler need not report an error if an operand's
2977
+ type is a < a href ="#Type_parameter_lists "> type parameter</ a > with an empty
2978
+ < a href ="#Interface_types "> type set</ a > . Functions with such type parameters
2979
+ cannot be < a href ="#Instantiations "> instantiated</ a > ; any attempt will lead
2980
+ to an error at the instantiation site.
2981
+ </ p >
2982
+
3040
2983
< h3 id ="Qualified_identifiers "> Qualified identifiers</ h3 >
3041
2984
3042
2985
< p >
@@ -3819,20 +3762,19 @@ <h3 id="Index_expressions">Index expressions</h3>
3819
3762
For < code > a</ code > of < a href ="#Type_parameter_lists "> type parameter type</ a > < code > P</ code > :
3820
3763
</ p >
3821
3764
< ul >
3822
- < li > < code > P</ code > must have < a href ="#Specific_types "> specific types</ a > .</ li >
3823
3765
< li > The index expression < code > a[x]</ code > must be valid for values
3824
- of all specific types of < code > P</ code > .</ li >
3825
- < li > The element types of all specific types of < code > P</ code > must be identical.
3766
+ of all types in < code > P</ code > 's type set .</ li >
3767
+ < li > The element types of all types in < code > P</ code > 's type set must be identical.
3826
3768
In this context, the element type of a string type is < code > byte</ code > .</ li >
3827
- < li > If there is a map type among the specific types of < code > P</ code > ,
3828
- all specific types must be map types, and the respective key types
3769
+ < li > If there is a map type in the type set of < code > P</ code > ,
3770
+ all types in that type set must be map types, and the respective key types
3829
3771
must be all identical.</ li >
3830
3772
< li > < code > a[x]</ code > is the array, slice, or string element at index < code > x</ code > ,
3831
3773
or the map element with key < code > x</ code > of the type argument
3832
3774
that < code > P</ code > is instantiated with, and the type of < code > a[x]</ code > is
3833
3775
the type of the (identical) element types.</ li >
3834
- < li > < code > a[x]</ code > may not be assigned to if the specific types of < code > P</ code >
3835
- include string types.
3776
+ < li > < code > a[x]</ code > may not be assigned to if < code > P</ code > 's type set
3777
+ includes string types.
3836
3778
</ ul >
3837
3779
3838
3780
< p >
@@ -4728,6 +4670,10 @@ <h3 id="Operators">Operators</h3>
4728
4670
to the type of the other operand.
4729
4671
</ p >
4730
4672
4673
+ < p > < b >
4674
+ [The rules for shifts need adjustments for type parameters. Issue #51182.]
4675
+ </ b > </ p >
4676
+
4731
4677
< p >
4732
4678
The right operand in a shift expression must have < a href ="#Numeric_types "> integer type</ a >
4733
4679
or be an untyped constant < a href ="#Representability "> representable</ a > by a
@@ -4832,9 +4778,8 @@ <h3 id="Arithmetic_operators">Arithmetic operators</h3>
4832
4778
</ pre >
4833
4779
4834
4780
< p >
4835
- Excluding shifts, if the operand type is a < a href ="#Type_parameter_lists "> type parameter</ a > ,
4836
- it must have < a href ="#Specific_types "> specific types</ a > , and the operator must
4837
- apply to each specific type.
4781
+ If the operand type is a < a href ="#Type_parameter_lists "> type parameter</ a > ,
4782
+ the operator must apply to each type in that type set.
4838
4783
The operands are represented as values of the type argument that the type parameter
4839
4784
is < a href ="#Instantiations "> instantiated</ a > with, and the operation is computed
4840
4785
with the precision of that type argument. For example, given the function:
@@ -4857,11 +4802,6 @@ <h3 id="Arithmetic_operators">Arithmetic operators</h3>
4857
4802
respectively, depending on the type argument for < code > F</ code > .
4858
4803
</ p >
4859
4804
4860
- < p >
4861
- For shifts, the < a href ="#Core_types "> core type</ a > of both operands must be
4862
- an integer.
4863
- </ p >
4864
-
4865
4805
< h4 id ="Integer_operators "> Integer operators</ h4 >
4866
4806
4867
4807
< p >
@@ -5374,23 +5314,23 @@ <h3 id="Conversions">Conversions</h3>
5374
5314
5375
5315
< p >
5376
5316
Additionally, if < code > T</ code > or < code > x</ code > 's type < code > V</ code > are type
5377
- parameters with < a href =" #Specific_types " > specific types </ a > , < code > x</ code >
5317
+ parameters, < code > x</ code >
5378
5318
can also be converted to type < code > T</ code > if one of the following conditions applies:
5379
5319
</ p >
5380
5320
5381
5321
< ul >
5382
5322
< li >
5383
5323
Both < code > V</ code > and < code > T</ code > are type parameters and a value of each
5384
- specific type of < code > V</ code > can be converted to each specific type
5385
- of < code > T </ code > .
5324
+ type in < code > V</ code > 's type set can be converted to each type in < code > T </ code > 's
5325
+ type set .
5386
5326
</ li >
5387
5327
< li >
5388
5328
Only < code > V</ code > is a type parameter and a value of each
5389
- specific type of < code > V</ code > can be converted to < code > T</ code > .
5329
+ type in < code > V</ code > 's type set can be converted to < code > T</ code > .
5390
5330
</ li >
5391
5331
< li >
5392
5332
Only < code > T</ code > is a type parameter and < code > x</ code > can be converted to each
5393
- specific type of < code > T</ code > .
5333
+ type in < code > T</ code > 's type set .
5394
5334
</ li >
5395
5335
</ ul >
5396
5336
@@ -7085,9 +7025,8 @@ <h3 id="Length_and_capacity">Length and capacity</h3>
7085
7025
7086
7026
< p >
7087
7027
If the argument type is a < a href ="#Type_parameter_lists "> type parameter</ a > < code > P</ code > ,
7088
- < code > P</ code > must have < a href ="#Specific_types "> specific types</ a > , and
7089
7028
the call < code > len(e)</ code > (or < code > cap(e)</ code > respectively) must be valid for
7090
- each specific type of < code > P</ code > .
7029
+ each type in < code > P</ code > 's type set .
7091
7030
The result is the length (or capacity, respectively) of the argument whose type
7092
7031
corresponds to the type argument with which < code > P</ code > was
7093
7032
< a href ="#Instantiations "> instantiated</ a > .
@@ -7309,8 +7248,7 @@ <h3 id="Deletion_of_map_elements">Deletion of map elements</h3>
7309
7248
7310
7249
< p >
7311
7250
If the type of < code > m</ code > is a < a href ="#Type_parameter_lists "> type parameter</ a > ,
7312
- it must have < a href ="#Specific_types "> specific types</ a > , all specific types
7313
- must be maps, and they must all have identical key types.
7251
+ all types in that type set must be maps, and they must all have identical key types.
7314
7252
</ p >
7315
7253
7316
7254
< p >
0 commit comments