From: Robert Griesemer Date: Wed, 24 Nov 2021 04:24:40 +0000 (-0800) Subject: spec: rules for index expressions, len, cap, with type parameter types X-Git-Tag: go1.18beta1~159 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=68da368a4ed0f6f47e841d75aaed0faf1dcf425c;p=gostls13.git spec: rules for index expressions, len, cap, with type parameter types We want to support some special cases for index expressions, len, and cap on operands of type parameters (such as indexing a value constrained by byte slices and strings), hence the extra rules. Change-Id: I4a07dc7e64bb47361b021d606c52eae1784d5430 Reviewed-on: https://go-review.googlesource.com/c/go/+/366814 Trust: Robert Griesemer Trust: Dan Scales Reviewed-by: Dan Scales --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 11f44d896d..8643d94476 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -2269,7 +2269,6 @@ An identifier is exported if both: All other identifiers are not exported.

-

Uniqueness of identifiers

@@ -3715,6 +3714,26 @@ For a of map type M: for the element type of M +

+For a of type parameter type P: +

+
    +
  • P must have specific types.
  • +
  • The index expression a[x] must be valid for values + of all specific types of P.
  • +
  • The element types of all specific types of P must be identical. + In this context, the element type of a string type is byte.
  • +
  • If there is a map type among the specific types of P, + all specific types must be map types, and the respective key types + must be all identical.
  • +
  • a[x] is the array, slice, or string element at index x, + or the map element with key x of the type argument + that P is instantiated with, and the type of a[x] is + the type of the (identical) element types.
  • +
  • a[x] may not be assigned to if the specific types of P + include string types. +
+

Otherwise a[x] is illegal.

@@ -6468,12 +6487,24 @@ len(s) string type string length in bytes []T slice length map[K]T map length (number of defined keys) chan T number of elements queued in channel buffer + type parameter see below cap(s) [n]T, *[n]T array length (== n) []T slice capacity chan T channel buffer capacity + type parameter see below +

+If the argument type is a type parameter P, +P must have specific types, and +the call len(e) (or cap(e) respectively) must be valid for +each specific type of P. +The result is the length (or capacity, respectively) of the argument whose type +corresponds to the type argument with which P was +instantiated. +

+

The capacity of a slice is the number of elements for which there is space allocated in the underlying array.