blob: 87c26937f3a47bbf2059fe0e09feaabc98f35523 [file] [log] [blame]
Russ Coxb9159722009-05-21 13:46:20 -07001// $G $D/$F.go && $L $F.$A && ./$A.out
2
3// Copyright 2009 The Go Authors. All rights reserved.
Russ Coxe508c5572009-05-06 17:05:55 -07004// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
Russ Coxc7d30bc2009-05-12 16:09:47 -07007// Implicit methods for embedded types.
8// Mixed pointer and non-pointer receivers.
9
Russ Coxe508c5572009-05-06 17:05:55 -070010package main
11
12type T int
13var nv, np int
14
15func (t T) V() {
16 if t != 42 {
17 panic(t)
18 }
19 nv++
20}
21
22func (t *T) P() {
23 if *t != 42 {
24 panic(t, *t)
25 }
26 np++
27}
28
29type V interface { V() }
30type P interface { P(); V() }
31
32type S struct {
33 T;
34}
35
36type SP struct {
37 *T;
38}
39
40func main() {
41 var t T;
42 var v V;
43 var p P;
44
45 t = 42;
46
47 t.P();
48 t.V();
49
50 v = t;
51 v.V();
52
53 p = &t;
54 p.P();
55 p.V();
56
57 v = &t;
58 v.V();
59
60// p = t; // ERROR
Russ Cox98811f42009-11-14 19:28:13 -080061 var i interface{} = t;
62 if _, ok := i.(P); ok {
63 panicln("dynamic i.(P) succeeded incorrectly");
64 }
Russ Coxe508c5572009-05-06 17:05:55 -070065
66// println("--struct--");
67 var s S;
68 s.T = 42;
69 s.P();
70 s.V();
71
72 v = s;
73 s.V();
74
75 p = &s;
76 p.P();
77 p.V();
78
79 v = &s;
80 v.V();
81
82// p = s; // ERROR
Russ Cox98811f42009-11-14 19:28:13 -080083 var j interface{} = s;
84 if _, ok := j.(P); ok {
85 panicln("dynamic j.(P) succeeded incorrectly");
86 }
Russ Coxe508c5572009-05-06 17:05:55 -070087
88// println("--struct pointer--");
89 var sp SP;
90 sp.T = &t;
91 sp.P();
92 sp.V();
93
94 v = sp;
95 sp.V();
96
97 p = &sp;
98 p.P();
99 p.V();
100
101 v = &sp;
102 v.V();
103
104 p = sp; // not error
105 p.P();
106 p.V();
107
108 if nv != 13 || np != 7 {
109 panicln("bad count", nv, np)
110 }
111}
112