Skip to content

Commit 0dffda1

Browse files
committed
spec: clarify "slice of bytes" and "slice of runes" through examples
The spec section on conversions uses the terms "slice of bytes" and "slice of runes". While not obviously clear, what is meant are slice types whose element types are byte or rune types; specifically the underlying types of the slices' element types must be byte or rune. Some of this was evident from the examples, but not all of it. Made this clearer by adding more examples illustrating various permitted conversions. Note that the 1.17 compiler did not accept the following conversions: string([]myByte{...}) string([]myRune{...}) myString([]myByte{...}) myString([]myRune{...}) (where myByte, myRune, and myString have underlying types of byte, rune, and string respectively) - it reported an internal error. But it did accept the inverse conversions: []myByte("...") []myRune("...") []myByte(myString("...")) []myRune(myString("...")) The 1.18 compiler made those conversions symmetric and they are now permitted in both directions. The extra examples reflect this reality. Fixes #23814. Change-Id: I5a1c200b45ddd0e8c0dc0d11da3a6c39cb2dc848 Reviewed-on: https://go-review.googlesource.com/c/go/+/412094 Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent c22a6c3 commit 0dffda1

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

doc/go_spec.html

Lines changed: 29 additions & 14 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 June 13, 2022",
3+
"Subtitle": "Version of June 14, 2022",
44
"Path": "/ref/spec"
55
}-->
66

@@ -5245,7 +5245,7 @@ <h3 id="Conversions">Conversions</h3>
52455245
float64(-1e-1000) // 0.0 of type float64
52465246
string('x') // "x" of type string
52475247
string(0x266c) // "♬" of type string
5248-
MyString("foo" + "bar") // "foobar" of type MyString
5248+
myString("foo" + "bar") // "foobar" of type myString
52495249
string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant
52505250
(*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
52515251
int(1.2) // illegal: 1.2 cannot be represented as an int
@@ -5428,8 +5428,9 @@ <h4 id="Conversions_to_and_from_a_string_type">Conversions to and from a string
54285428
string('a') // "a"
54295429
string(-1) // "\ufffd" == "\xef\xbf\xbd"
54305430
string(0xf8) // "\u00f8" == "ø" == "\xc3\xb8"
5431-
type MyString string
5432-
MyString(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5"
5431+
5432+
type myString string
5433+
myString(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5"
54335434
</pre>
54345435
</li>
54355436

@@ -5442,8 +5443,12 @@ <h4 id="Conversions_to_and_from_a_string_type">Conversions to and from a string
54425443
string([]byte{}) // ""
54435444
string([]byte(nil)) // ""
54445445

5445-
type MyBytes []byte
5446-
string(MyBytes{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø"
5446+
type bytes []byte
5447+
string(bytes{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø"
5448+
5449+
type myByte byte
5450+
string([]myByte{'w', 'o', 'r', 'l', 'd', '!'}) // "world!"
5451+
myString([]myByte{'\xf0', '\x9f', '\x8c', '\x8d'}) // "🌍"
54475452
</pre>
54485453
</li>
54495454

@@ -5457,8 +5462,12 @@ <h4 id="Conversions_to_and_from_a_string_type">Conversions to and from a string
54575462
string([]rune{}) // ""
54585463
string([]rune(nil)) // ""
54595464

5460-
type MyRunes []rune
5461-
string(MyRunes{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔"
5465+
type runes []rune
5466+
string(runes{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔"
5467+
5468+
type myRune rune
5469+
string([]myRune{0x266b, 0x266c}) // "\u266b\u266c" == "♫♬"
5470+
myString([]myRune{0x1F30E}) // "\U0001f30e" == "🌎"
54625471
</pre>
54635472
</li>
54645473

@@ -5467,10 +5476,13 @@ <h4 id="Conversions_to_and_from_a_string_type">Conversions to and from a string
54675476
yields a slice whose successive elements are the bytes of the string.
54685477

54695478
<pre>
5470-
[]byte("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
5471-
[]byte("") // []byte{}
5479+
[]byte("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
5480+
[]byte("") // []byte{}
54725481

5473-
MyBytes("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
5482+
bytes("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
5483+
5484+
[]myByte("world!") // []myByte{'w', 'o', 'r', 'l', 'd', '!'}
5485+
[]myByte(myString("🌏")) // []myByte{'\xf0', '\x9f', '\x8c', '\x8f'}
54745486
</pre>
54755487
</li>
54765488

@@ -5479,10 +5491,13 @@ <h4 id="Conversions_to_and_from_a_string_type">Conversions to and from a string
54795491
yields a slice containing the individual Unicode code points of the string.
54805492

54815493
<pre>
5482-
[]rune(MyString("白鵬翔")) // []rune{0x767d, 0x9d6c, 0x7fd4}
5483-
[]rune("") // []rune{}
5494+
[]rune(myString("白鵬翔")) // []rune{0x767d, 0x9d6c, 0x7fd4}
5495+
[]rune("") // []rune{}
5496+
5497+
runes("白鵬翔") // []rune{0x767d, 0x9d6c, 0x7fd4}
54845498

5485-
MyRunes("白鵬翔") // []rune{0x767d, 0x9d6c, 0x7fd4}
5499+
[]myRune("♫♬") // []myRune{0x266b, 0x266c}
5500+
[]myRune(myString("🌐")) // []myRune{0x1f310}
54865501
</pre>
54875502
</li>
54885503
</ol>

0 commit comments

Comments
 (0)