OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package main | 5 package main |
6 | 6 |
7 import ( | 7 import ( |
8 "./p" | 8 "./p" |
9 ) | 9 ) |
10 | 10 |
11 type Exported interface { | 11 type Exported interface { |
12 private() | 12 private() |
13 } | 13 } |
14 | 14 |
15 type Implementation struct{} | 15 type Implementation struct{} |
16 | 16 |
17 func (p *Implementation) private() { println("main.Implementation.private()") } | 17 func (p *Implementation) private() {} |
18 | 18 |
19 | 19 |
20 func main() { | 20 func main() { |
21 // nothing unusual here | 21 // nothing unusual here |
22 var x Exported | 22 var x Exported |
23 x = new(Implementation) | 23 x = new(Implementation) |
24 x.private() // main.Implementation.private() | 24 x.private() // main.Implementation.private() |
25 | 25 |
26 // same here - should be and is legal | 26 // same here - should be and is legal |
27 var px p.Exported | 27 var px p.Exported |
28 px = p.X | 28 px = p.X |
29 ········ | 29 ········ |
30 // this assignment is correctly illegal: | 30 // this assignment is correctly illegal: |
31 // px.private undefined (cannot refer to unexported field or method
private) | 31 // px.private undefined (cannot refer to unexported field or method
private) |
32 // px.private() | 32 // px.private() |
33 | 33 |
34 // this assignment is correctly illegal: | 34 // this assignment is correctly illegal: |
35 // *Implementation does not implement p.Exported (missing p.private
method) | 35 // *Implementation does not implement p.Exported (missing p.private
method) |
36 // px = new(Implementation) | 36 // px = new(Implementation) |
37 | 37 |
38 // this assignment is correctly illegal: | 38 // this assignment is correctly illegal: |
39 // p.Exported does not implement Exported (missing private method) | 39 // p.Exported does not implement Exported (missing private method) |
40 // x = px | 40 // x = px |
41 | 41 |
42 // this assignment unexpectedly compiles and then executes | 42 // this assignment unexpectedly compiles and then executes |
| 43 defer func() { |
| 44 recover() |
| 45 }() |
43 x = px.(Exported) | 46 x = px.(Exported) |
| 47 ········ |
| 48 println("should not get this far") |
44 | 49 |
45 // this is a legitimate call, but because of the previous assignment, | 50 // this is a legitimate call, but because of the previous assignment, |
46 // it invokes the method private in p! | 51 // it invokes the method private in p! |
47 x.private() // p.Implementation.private() | 52 x.private() // p.Implementation.private() |
48 } | 53 } |
OLD | NEW |