From 3bb90a278a09c889fe936b2c5053116e48312ba2 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Sun, 13 Feb 2022 21:27:58 -0800 Subject: [PATCH] spec: clarifications based on feedback This change includes several smaller changes based on feedback received so far. These changes were reviewed at CL 385536. The only additional change here is to the current date in the subtitle. Change-Id: I653eb4a143e3b86c5357a2fd3b19168419c9f432 Reviewed-on: https://go-review.googlesource.com/c/go/+/390634 Trust: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor --- doc/go_spec.html | 82 ++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 44 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 6c6f982854..e8061f94b9 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -987,7 +987,7 @@ built-in function cap(a).

-A new, initialized slice value for a given element type T is +A new, initialized slice value for a given element type T may be made using the built-in function make, which takes a slice type @@ -1422,7 +1422,7 @@ interface { ~int } -// An interface representing all types with underlying type int which implement the String method. +// An interface representing all types with underlying type int that implement the String method. interface { ~int String() string @@ -1455,32 +1455,32 @@ Union elements denote unions of type sets:

-// The Floats interface represents all floating-point types
+// The Float interface represents all floating-point types
 // (including any named types whose underlying types are
 // either float32 or float64).
-type Floats interface {
+type Float interface {
 	~float32 | ~float64
 }
 

-In a union, a term cannot be a type parameter, and the type sets of all +In a union, a term cannot be a type parameter, and the type sets of all non-interface terms must be pairwise disjoint (the pairwise intersection of the type sets must be empty). Given a type parameter P:

 interface {
-	P                 // illegal: the term P is a type parameter
-	int | P           // illegal: the term P is a type parameter
-	~int | MyInt      // illegal: the type sets for ~int and MyInt are not disjoint (~int includes MyInt)
-	float32 | Floats  // overlapping type sets but Floats is an interface
+	P                // illegal: P is a type parameter
+	int | P          // illegal: P is a type parameter
+	~int | MyInt     // illegal: the type sets for ~int and MyInt are not disjoint (~int includes MyInt)
+	float32 | Float  // overlapping type sets but Float is an interface
 }
 

Implementation restriction: -A union with more than one term cannot contain the +A union (with more than one term) cannot contain the predeclared identifier comparable or interfaces that specify methods, or embed comparable or interfaces that specify methods. @@ -1494,12 +1494,12 @@ non-interface types.

-var x Floats                     // illegal: Floats is not a basic interface
+var x Float                     // illegal: Float is not a basic interface
 
-var x interface{} = Floats(nil)  // illegal
+var x interface{} = Float(nil)  // illegal
 
 type Floatish struct {
-	f Floats                 // illegal
+	f Float                 // illegal
 }
 
@@ -1545,7 +1545,7 @@ A type T implements an interface I if

-A value x of type T implements an interface if T +A value of type T implements an interface if T implements the interface.

@@ -1701,10 +1701,9 @@ Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself. Otherwise, T's underlying type is the underlying type of the -type to which T refers in its type -declaration. The underlying type of a type parameter is the -underlying type of its type constraint, which -is always an interface. +type to which T refers in its declaration. +For a type parameter that is the underlying type of its +type constraint, which is always an interface.

@@ -1755,7 +1754,7 @@ direction.
 
 
 

-All other interfaces don't have a core type. +No other interfaces have a core type.

@@ -1795,7 +1794,7 @@ interface{ ~[]*data; String() string } // []*data

-Examples of interfaces whithout core types: +Examples of interfaces without core types:

@@ -1973,21 +1972,21 @@ defined type while the latter is a type literal
 

Assignability

-A value x is assignable to a variable of type T +A value x of type V is assignable to a variable of type T ("x is assignable to T") if one of the following conditions applies:

  • -x's type is identical to T. +V and T are identical.
  • -x's type V and T have identical +V and T have identical underlying types and at least one of V or T is not a named type.
  • -x's type V and T are channel types with +V and T are channel types with identical element types, V is a bidirectional channel, and at least one of V or T is not a named type.
  • @@ -2220,13 +2219,13 @@ Go is lexically scoped using blocks:
  • The scope of an identifier denoting a method receiver, function parameter, or result variable is the function body.
  • -
  • The scope of an identifier denoting a type parameter of a generic function +
  • The scope of an identifier denoting a type parameter of a function or declared by a method receiver is the function body and all parameter lists of the function.
  • -
  • The scope of an identifier denoting a type parameter of a generic type - begins after the name of the generic type and ends at the end +
  • The scope of an identifier denoting a type parameter of a type + begins after the name of the type and ends at the end of the TypeSpec.
  • The scope of a constant or variable identifier declared @@ -2512,7 +2511,7 @@ type ( type TreeNode struct { left, right *TreeNode - value *Comparable + value any } type Block interface { @@ -2584,15 +2583,10 @@ type List[T any] struct { next *List[T] value T } - -type Tree[T constraints.Ordered] struct { - left, right *Tree[T] - value T -}

-The given type cannot be a type parameter in a type definition. +In a type definition the given type cannot be a type parameter.

@@ -2604,8 +2598,8 @@ func f[T any]() {
 

-A generic type may also have methods associated with it. In this case, -the method receivers must declare the same number of type parameters as +A generic type may also have methods associated with it. +In this case, the method receivers must declare the same number of type parameters as present in the generic type definition.

@@ -2899,12 +2893,12 @@ func IndexRune(s string, r rune) int {

If the function declaration specifies type parameters, the function name denotes a generic function. -Generic functions must be instantiated when they -are used. +A generic function must be instantiated before it can be +called or used as a value.

-func min[T constraints.Ordered](x, y T) T {
+func min[T ~int|~float64](x, y T) T {
 	if x < y {
 		return x
 	}
@@ -2963,7 +2957,7 @@ the non-blank method and field names must be distinct.
 

-Given defined type Point, the declarations +Given defined type Point the declarations

@@ -3758,7 +3752,7 @@ The following rules apply:
 

-If a is not a map: +If a is neither a map nor a type parameter:

  • the index x must be an untyped constant or its @@ -4298,7 +4292,7 @@ inferrable from the ordinary (non-type) function arguments.

    -func min[T constraints.Ordered](x, y T) T { … }
    +func min[T ~int|~float64](x, y T) T { … }
     
     f := min                   // illegal: min must be instantiated when used without being called
     minInt := min[int]         // minInt has type func(x, y int) int
    @@ -4550,7 +4544,7 @@ Example:
     

    -func min[T constraints.Ordered](x, y T) T
    +func min[T ~int|~float64](x, y T) T
     
     var x int
     min(x, 2.0)    // T is int, inferred from typed argument x; 2.0 is assignable to int
    -- 
    2.50.0