1
0
mirror of https://github.com/golang/go synced 2024-11-25 06:27:57 -07:00

Go spec: map indexing never raises a runtime exception.

Also: Actual map key must be assignment-compatible with
formal map key type.

Fixes #357.

R=r, iant, rsc, ken2
CC=golang-dev
https://golang.org/cl/673042
This commit is contained in:
Robert Griesemer 2010-03-23 14:01:51 -07:00
parent a65a56ec1f
commit 29f1ca528b

View File

@ -2394,9 +2394,10 @@ where <code>A</code> is an <a href="#Array_types">array type</a>,
or for <code>a</code> of type <code>S</code> where <code>S</code> is a <a href="#Slice_types">slice type</a>: or for <code>a</code> of type <code>S</code> where <code>S</code> is a <a href="#Slice_types">slice type</a>:
</p> </p>
<ul> <ul>
<li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code> <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code></li>
<li><code>a[x]</code> is the array element at index <code>x</code> and the type of <li><code>a[x]</code> is the array element at index <code>x</code> and the type of
<code>a[x]</code> is the element type of <code>A</code> <code>a[x]</code> is the element type of <code>A</code></li>
<li>if the index <code>x</code> is out of range, a run-time exception occurs</li>
</ul> </ul>
<p> <p>
@ -2404,10 +2405,11 @@ For <code>a</code> of type <code>T</code>
where <code>T</code> is a <a href="#String_types">string type</a>: where <code>T</code> is a <a href="#String_types">string type</a>:
</p> </p>
<ul> <ul>
<li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code> <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code></li>
<li><code>a[x]</code> is the byte at index <code>x</code> and the type of <li><code>a[x]</code> is the byte at index <code>x</code> and the type of
<code>a[x]</code> is <code>byte</code> <code>a[x]</code> is <code>byte</code></li>
<li><code>a[x]</code> may not be assigned to <li><code>a[x]</code> may not be assigned to
<li>if the index <code>x</code> is out of range, a run-time exception occurs</li>
</ul> </ul>
<p> <p>
@ -2415,38 +2417,38 @@ For <code>a</code> of type <code>M</code>
where <code>M</code> is a <a href="#Map_types">map type</a>: where <code>M</code> is a <a href="#Map_types">map type</a>:
</p> </p>
<ul> <ul>
<li><code>x</code>'s type must be compatible with the key type of <code>M</code> <li><code>x</code>'s type must be
and the map must contain an entry with key <code>x</code> (but see special forms below) <a href="#Assignment_compatibility">assignment compatible</a>
<li><code>a[x]</code> is the map value with key <code>x</code> with the key type of <code>M</code></li>
and the type of <code>a[x]</code> is the value type of <code>M</code> <li>if the map contains an entry with key <code>x</code>,
<code>a[x]</code> is the map value with key <code>x</code>
and the type of <code>a[x]</code> is the value type of <code>M</code></li>
<li>if the map does not contain such an entry,
<code>a[x]</code> is the <a href="#The_zero_value">zero value</a>
for the value type of <code>M</code></li>
</ul> </ul>
<p> <p>
Otherwise <code>a[x]</code> is illegal. If the index or key is out of range evaluating Otherwise <code>a[x]</code> is illegal.
an otherwise legal index expression, a run-time exception occurs.
</p> </p>
<p> <p>
However, if an index expression on a map <code>a</code> of type <code>map[K] V</code> An index expression on a map <code>a</code> of type <code>map[K]V</code>
is used in an assignment or initialization of the form may be used in an assignment or initialization of the special form
</p> </p>
<pre> <pre>
r, ok = a[x] v, ok = a[x]
r, ok := a[x] v, ok := a[x]
var r, ok = a[x] var v, ok = a[x]
</pre> </pre>
<p> <p>
the result of the index expression is a pair of values with types where the result of the index expression is a pair of values with types
<code>(V, bool)</code>. <code>(V, bool)</code>. In this form, the value of <code>ok</code> is
If the key is present in the map, <code>true</code> if the key <code>x</code> is present in the map, and
the expression returns the pair <code>(a[x], true)</code>; <code>false</code> otherwise. The value of <code>v</code> is the value
otherwise it returns <code>(Z, false)</code> where <code>Z</code> is <code>a[x]</code> as in the single-result form.
the <a href="#The_zero_value">zero value</a> for <code>V</code>.
No run-time exception occurs in this case.
The index expression in this construct thus acts like a function call
returning a value and a boolean indicating success. (§<a href="#Assignments">Assignments</a>)
</p> </p>
<p> <p>
@ -2454,7 +2456,7 @@ Similarly, if an assignment to a map has the special form
</p> </p>
<pre> <pre>
a[x] = r, ok a[x] = v, ok
</pre> </pre>
<p> <p>
@ -2464,6 +2466,7 @@ the entry for key <code>x</code> is deleted from the map; if
a regular assignment to an element of the map. a regular assignment to an element of the map.
</p> </p>
<h3 id="Slices">Slices</h3> <h3 id="Slices">Slices</h3>
<p> <p>