Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(97)

Delta Between Two Patch Sets: doc/go_spec.html

Issue 6862046: code review 6862046: spec: receiver types in method expressions can be paren... (Closed)
Left Patch Set: diff -r 5eac1a2d6fc3 https://code.google.com/p/go Created 12 years, 3 months ago
Right Patch Set: diff -r 992bfc985165 https://code.google.com/p/go Created 12 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 <!--{ 1 <!--{
2 "Title": "The Go Programming Language Specification", 2 "Title": "The Go Programming Language Specification",
3 » "Subtitle": "Version of December 5, 2012", 3 » "Subtitle": "Version of December 6, 2012",
4 "Path": "/ref/spec" 4 "Path": "/ref/spec"
5 }--> 5 }-->
6 6
7 <!-- 7 <!--
8 TODO 8 TODO
9 [ ] need language about function/method calls and parameter passing rules 9 [ ] need language about function/method calls and parameter passing rules
10 [ ] last paragraph of #Assignments (constant promotion) should be elsewhere 10 [ ] last paragraph of #Assignments (constant promotion) should be elsewhere
11 and mention assignment to empty interface. 11 and mention assignment to empty interface.
12 [ ] need to say something about "scope" of selectors? 12 [ ] need to say something about "scope" of selectors?
13 [ ] clarify what a field name is in struct declarations 13 [ ] clarify what a field name is in struct declarations
(...skipping 2647 matching lines...) Expand 10 before | Expand all | Expand 10 after
2661 2661
2662 <p> 2662 <p>
2663 asserts that <code>x</code> is not <code>nil</code> 2663 asserts that <code>x</code> is not <code>nil</code>
2664 and that the value stored in <code>x</code> is of type <code>T</code>. 2664 and that the value stored in <code>x</code> is of type <code>T</code>.
2665 The notation <code>x.(T)</code> is called a <i>type assertion</i>. 2665 The notation <code>x.(T)</code> is called a <i>type assertion</i>.
2666 </p> 2666 </p>
2667 <p> 2667 <p>
2668 More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> a sserts 2668 More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> a sserts
2669 that the dynamic type of <code>x</code> is <a href="#Type_identity">identical</a > 2669 that the dynamic type of <code>x</code> is <a href="#Type_identity">identical</a >
2670 to the type <code>T</code>. 2670 to the type <code>T</code>.
2671 In this case, <code>T</code> must <a href="#Method_sets">implement</a> the (inte rface) type of <code>x</code>;
2672 otherwise the type assertion is invalid since it is not possible for <code>x</co de>
2673 to store a value of type <code>T</code>.
2671 If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dyna mic type 2674 If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dyna mic type
2672 of <code>x</code> implements the interface <code>T</code> (§<a href="#Interface_ types">Interface types</a>). 2675 of <code>x</code> implements the interface <code>T</code>.
2673 </p> 2676 </p>
2674 <p> 2677 <p>
2675 If the type assertion holds, the value of the expression is the value 2678 If the type assertion holds, the value of the expression is the value
2676 stored in <code>x</code> and its type is <code>T</code>. If the type assertion i s false, 2679 stored in <code>x</code> and its type is <code>T</code>. If the type assertion i s false,
2677 a <a href="#Run_time_panics">run-time panic</a> occurs. 2680 a <a href="#Run_time_panics">run-time panic</a> occurs.
2678 In other words, even though the dynamic type of <code>x</code> 2681 In other words, even though the dynamic type of <code>x</code>
2679 is known only at run time, the type of <code>x.(T)</code> is 2682 is known only at run time, the type of <code>x.(T)</code> is
2680 known to be <code>T</code> in a correct program. 2683 known to be <code>T</code> in a correct program.
2681 </p> 2684 </p>
2682 <p> 2685
2683 If a type assertion is used in an assignment or initialization of the form 2686 <pre>
2687 var x interface{} = 7 // x has dynamic type int and value 7
2688 i := x.(int) // i has type int and value 7
2689
2690 type I interface { m() }
2691 var y I
2692 s := y.(string) // illegal: string does not implement I (missing method m )
2693 r := y.(io.Reader) // r has type io.Reader and y must implement both I and i o.Reader
2694 </pre>
2695
2696 <p>
2697 If a type assertion is used in an <a href="#Assignments">assignment</a> or initi alization of the form
2684 </p> 2698 </p>
2685 2699
2686 <pre> 2700 <pre>
2687 v, ok = x.(T) 2701 v, ok = x.(T)
2688 v, ok := x.(T) 2702 v, ok := x.(T)
2689 var v, ok = x.(T) 2703 var v, ok = x.(T)
2690 </pre> 2704 </pre>
2691 2705
2692 <p> 2706 <p>
2693 the result of the assertion is a pair of values with types <code>(T, bool)</code >. 2707 the result of the assertion is a pair of values with types <code>(T, bool)</code >.
2694 If the assertion holds, the expression returns the pair <code>(x.(T), true)</cod e>; 2708 If the assertion holds, the expression returns the pair <code>(x.(T), true)</cod e>;
2695 otherwise, the expression returns <code>(Z, false)</code> where <code>Z</code> 2709 otherwise, the expression returns <code>(Z, false)</code> where <code>Z</code>
2696 is the <a href="#The_zero_value">zero value</a> for type <code>T</code>. 2710 is the <a href="#The_zero_value">zero value</a> for type <code>T</code>.
2697 No run-time panic occurs in this case. 2711 No run-time panic occurs in this case.
2698 The type assertion in this construct thus acts like a function call 2712 The type assertion in this construct thus acts like a function call
2699 returning a value and a boolean indicating success. (§<a href="#Assignments">As signments</a>) 2713 returning a value and a boolean indicating success.
2700 </p> 2714 </p>
2701 2715
2702 2716
2703 <h3 id="Calls">Calls</h3> 2717 <h3 id="Calls">Calls</h3>
2704 2718
2705 <p> 2719 <p>
2706 Given an expression <code>f</code> of function type 2720 Given an expression <code>f</code> of function type
2707 <code>F</code>, 2721 <code>F</code>,
2708 </p> 2722 </p>
2709 2723
(...skipping 628 matching lines...) Expand 10 before | Expand all | Expand 10 after
3338 </pre> 3352 </pre>
3339 3353
3340 <p> 3354 <p>
3341 That function may be called normally with an explicit receiver, so 3355 That function may be called normally with an explicit receiver, so
3342 these five invocations are equivalent: 3356 these five invocations are equivalent:
3343 </p> 3357 </p>
3344 3358
3345 <pre> 3359 <pre>
3346 t.Mv(7) 3360 t.Mv(7)
3347 T.Mv(t, 7) 3361 T.Mv(t, 7)
3348 (T).Mv(t, t) // receiver types in method expressions may be parenthes ized 3362 (T).Mv(t, t)
iant 2012/12/05 23:50:29 I think I would omit the comment on this line.
quarnster 2012/12/06 20:05:00 Shouldn't this be "(T).Mv(t, 7)"?
gri 2012/12/06 20:11:42 good catch - will fix in the next spec CL
3349 f1 := T.Mv; f1(t, 7) 3363 f1 := T.Mv; f1(t, 7)
3350 f2 := (T).Mv; f2(t, 7) 3364 f2 := (T).Mv; f2(t, 7)
3351 </pre> 3365 </pre>
3352 3366
3353 <p> 3367 <p>
3354 Similarly, the expression 3368 Similarly, the expression
3355 </p> 3369 </p>
3356 3370
3357 <pre> 3371 <pre>
3358 (*T).Mp 3372 (*T).Mp
(...skipping 795 matching lines...) Expand 10 before | Expand all | Expand 10 after
4154 case x == 4: f3() 4168 case x == 4: f3()
4155 } 4169 }
4156 </pre> 4170 </pre>
4157 4171
4158 <h4 id="Type_switches">Type switches</h4> 4172 <h4 id="Type_switches">Type switches</h4>
4159 4173
4160 <p> 4174 <p>
4161 A type switch compares types rather than values. It is otherwise similar 4175 A type switch compares types rather than values. It is otherwise similar
4162 to an expression switch. It is marked by a special switch expression that 4176 to an expression switch. It is marked by a special switch expression that
4163 has the form of a <a href="#Type_assertions">type assertion</a> 4177 has the form of a <a href="#Type_assertions">type assertion</a>
4164 using the reserved word <code>type</code> rather than an actual type. 4178 using the reserved word <code>type</code> rather than an actual type:
4165 Cases then match literal types against the dynamic type of the expression 4179 </p>
4166 in the type assertion. 4180
4181 <pre>
4182 switch x.(type) {
4183 // cases
4184 }
4185 </pre>
4186
4187 <p>
4188 Cases then match actual types <code>T</code> against the dynamic type of the
4189 expression <code>x</code>. As with type assertions, <code>x</code> must be of
4190 <a href="#Interface_types">interface type</a>, and each non-interface type
4191 <code>T</code> listed in a case must implement the type of <code>x</code>.
4167 </p> 4192 </p>
4168 4193
4169 <pre class="ebnf"> 4194 <pre class="ebnf">
4170 TypeSwitchStmt = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClau se } "}" . 4195 TypeSwitchStmt = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClau se } "}" .
4171 TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" . 4196 TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" .
4172 TypeCaseClause = TypeSwitchCase ":" { Statement ";" } . 4197 TypeCaseClause = TypeSwitchCase ":" { Statement ";" } .
4173 TypeSwitchCase = "case" TypeList | "default" . 4198 TypeSwitchCase = "case" TypeList | "default" .
4174 TypeList = Type { "," Type } . 4199 TypeList = Type { "," Type } .
4175 </pre> 4200 </pre>
4176 4201
(...skipping 15 matching lines...) Expand all
4192 </p> 4217 </p>
4193 4218
4194 <p> 4219 <p>
4195 Given an expression <code>x</code> of type <code>interface{}</code>, 4220 Given an expression <code>x</code> of type <code>interface{}</code>,
4196 the following type switch: 4221 the following type switch:
4197 </p> 4222 </p>
4198 4223
4199 <pre> 4224 <pre>
4200 switch i := x.(type) { 4225 switch i := x.(type) {
4201 case nil: 4226 case nil:
4202 » printString("x is nil") 4227 » printString("x is nil") // type of i is type of x (interf ace{})
4203 case int: 4228 case int:
4204 » printInt(i) // i is an int 4229 » printInt(i) // type of i is int
4205 case float64: 4230 case float64:
4206 » printFloat64(i) // i is a float64 4231 » printFloat64(i) // type of i is float64
4207 case func(int) float64: 4232 case func(int) float64:
4208 » printFunction(i) // i is a function 4233 » printFunction(i) // type of i is func(int) float64
4209 case bool, string: 4234 case bool, string:
4210 » printString("type is bool or string") // i is an interface{} 4235 » printString("type is bool or string") // type of i is type of x (interf ace{})
4211 default: 4236 default:
4212 » printString("don't know the type") 4237 » printString("don't know the type") // type of i is type of x (interf ace{})
4213 } 4238 }
4214 </pre> 4239 </pre>
4215 4240
4216 <p> 4241 <p>
4217 could be rewritten: 4242 could be rewritten:
4218 </p> 4243 </p>
4219 4244
4220 <pre> 4245 <pre>
4221 v := x // x is evaluated exactly once 4246 v := x // x is evaluated exactly once
4222 if v == nil { 4247 if v == nil {
4248 i := v // type of i is type of x (interf ace{})
4223 printString("x is nil") 4249 printString("x is nil")
4224 } else if i, isInt := v.(int); isInt { 4250 } else if i, isInt := v.(int); isInt {
4225 » printInt(i) // i is an int 4251 » printInt(i) // type of i is int
4226 } else if i, isFloat64 := v.(float64); isFloat64 { 4252 } else if i, isFloat64 := v.(float64); isFloat64 {
4227 » printFloat64(i) // i is a float64 4253 » printFloat64(i) // type of i is float64
4228 } else if i, isFunc := v.(func(int) float64); isFunc { 4254 } else if i, isFunc := v.(func(int) float64); isFunc {
4229 » printFunction(i) // i is a function 4255 » printFunction(i) // type of i is func(int) float64
4230 } else { 4256 } else {
4231 » i1, isBool := v.(bool) 4257 » _, isBool := v.(bool)
4232 » i2, isString := v.(string) 4258 » _, isString := v.(string)
4233 if isBool || isString { 4259 if isBool || isString {
4234 » » i := v 4260 » » i := v // type of i is type of x (interf ace{})
4235 » » printString("type is bool or string") // i is an interface{} 4261 » » printString("type is bool or string")
4236 } else { 4262 } else {
4237 » » i := v 4263 » » i := v // type of i is type of x (interf ace{})
4238 » » printString("don't know the type") // i is an interface{} 4264 » » printString("don't know the type")
4239 } 4265 }
4240 } 4266 }
4241 </pre> 4267 </pre>
4242 4268
4243 <p> 4269 <p>
4244 The type switch guard may be preceded by a simple statement, which 4270 The type switch guard may be preceded by a simple statement, which
4245 executes before the guard is evaluated. 4271 executes before the guard is evaluated.
4246 </p> 4272 </p>
4247 4273
4248 <p> 4274 <p>
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
4496 statement, the channel expressions are evaluated in top-to-bottom order, along w ith 4522 statement, the channel expressions are evaluated in top-to-bottom order, along w ith
4497 any expressions that appear on the right hand side of send statements. 4523 any expressions that appear on the right hand side of send statements.
4498 A channel may be <code>nil</code>, 4524 A channel may be <code>nil</code>,
4499 which is equivalent to that case not 4525 which is equivalent to that case not
4500 being present in the select statement 4526 being present in the select statement
4501 except, if a send, its expression is still evaluated. 4527 except, if a send, its expression is still evaluated.
4502 If any of the resulting operations can proceed, one of those is 4528 If any of the resulting operations can proceed, one of those is
4503 chosen and the corresponding communication and statements are 4529 chosen and the corresponding communication and statements are
4504 evaluated. Otherwise, if there is a default case, that executes; 4530 evaluated. Otherwise, if there is a default case, that executes;
4505 if there is no default case, the statement blocks until one of the communication s can 4531 if there is no default case, the statement blocks until one of the communication s can
4506 complete. 4532 complete. There can be at most one default case and it may appear anywhere in th e
4533 "select" statement.
4507 If there are no cases with non-<code>nil</code> channels, 4534 If there are no cases with non-<code>nil</code> channels,
4508 the statement blocks forever. 4535 the statement blocks forever.
4509 Even if the statement blocks, 4536 Even if the statement blocks,
4510 the channel and send expressions are evaluated only once, 4537 the channel and send expressions are evaluated only once,
4511 upon entering the select statement. 4538 upon entering the select statement.
4512 </p> 4539 </p>
4513 <p> 4540 <p>
4514 Since all the channels and send expressions are evaluated, any side 4541 Since all the channels and send expressions are evaluated, any side
4515 effects in that evaluation will occur for all the communications 4542 effects in that evaluation will occur for all the communications
4516 in the "select" statement. 4543 in the "select" statement.
(...skipping 1118 matching lines...) Expand 10 before | Expand all | Expand 10 after
5635 </li> 5662 </li>
5636 5663
5637 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as 5664 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
5638 <code>unsafe.Alignof(x[0])</code>, but at least 1. 5665 <code>unsafe.Alignof(x[0])</code>, but at least 1.
5639 </li> 5666 </li>
5640 </ol> 5667 </ol>
5641 5668
5642 <p> 5669 <p>
5643 A struct or array type has size zero if it contains no fields (or elements, resp ectively) that have a size greater than zero. Two distinct zero-size variables m ay have the same address in memory. 5670 A struct or array type has size zero if it contains no fields (or elements, resp ectively) that have a size greater than zero. Two distinct zero-size variables m ay have the same address in memory.
5644 </p> 5671 </p>
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b