LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 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 try contains the executable part of the gotry command. | 5 // Package try contains the executable part of the gotry command. |
6 // It is not intended for general use. | 6 // It is not intended for general use. |
7 package try | 7 package try |
8 | 8 |
9 import ( | 9 import ( |
10 "fmt" | 10 "fmt" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 rfn := method.Func | 84 rfn := method.Func |
85 typ := method.Type | 85 typ := method.Type |
86 name := method.Name | 86 name := method.Name |
87 tryOneFunction(pkg, firstArg, name, typ, rfn, args) | 87 tryOneFunction(pkg, firstArg, name, typ, rfn, args) |
88 } | 88 } |
89 | 89 |
90 // tryFunction sees if fn satisfies the arguments. | 90 // tryFunction sees if fn satisfies the arguments. |
91 func tryFunction(pkg, name string, fn interface{}, args []interface{}) { | 91 func tryFunction(pkg, name string, fn interface{}, args []interface{}) { |
92 defer func() { recover() }() | 92 defer func() { recover() }() |
93 rfn := reflect.NewValue(fn).(*reflect.FuncValue) | 93 rfn := reflect.NewValue(fn).(*reflect.FuncValue) |
94 » typ := rfn.Type().(*reflect.FuncType) | 94 » typ := rfn.Type() |
95 tryOneFunction(pkg, "", name, typ, rfn, args) | 95 tryOneFunction(pkg, "", name, typ, rfn, args) |
96 } | 96 } |
97 | 97 |
98 // tryOneFunction is the common code for tryMethod and tryFunction. | 98 // tryOneFunction is the common code for tryMethod and tryFunction. |
99 func tryOneFunction(pkg, firstArg, name string, typ *reflect.FuncType, rfn *refl
ect.FuncValue, args []interface{}) { | 99 func tryOneFunction(pkg, firstArg, name string, typ reflect.Type, rfn *reflect.F
uncValue, args []interface{}) { |
100 // Any results? | 100 // Any results? |
101 if typ.NumOut() == 0 { | 101 if typ.NumOut() == 0 { |
102 return // Nothing to do. | 102 return // Nothing to do. |
103 } | 103 } |
104 // Right number of arguments + results? | 104 // Right number of arguments + results? |
105 if typ.NumIn()+typ.NumOut() != len(args) { | 105 if typ.NumIn()+typ.NumOut() != len(args) { |
106 return | 106 return |
107 } | 107 } |
108 // Right argument and result types? | 108 // Right argument and result types? |
109 for i, a := range args { | 109 for i, a := range args { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 fmt.Fprintf(output, " // godoc %s %s\n", pkg, name) | 159 fmt.Fprintf(output, " // godoc %s %s\n", pkg, name) |
160 } | 160 } |
161 | 161 |
162 // compatible reports whether the argument is compatible with the type. | 162 // compatible reports whether the argument is compatible with the type. |
163 func compatible(arg interface{}, typ reflect.Type) bool { | 163 func compatible(arg interface{}, typ reflect.Type) bool { |
164 if reflect.Typeof(arg) == typ { | 164 if reflect.Typeof(arg) == typ { |
165 return true | 165 return true |
166 } | 166 } |
167 if arg == nil { | 167 if arg == nil { |
168 // nil is OK if the type is an interface. | 168 // nil is OK if the type is an interface. |
169 » » if _, ok := typ.(*reflect.InterfaceType); ok { | 169 » » if typ.Kind() == reflect.Interface { |
170 return true | 170 return true |
171 } | 171 } |
172 } | 172 } |
173 return false | 173 return false |
174 } | 174 } |
LEFT | RIGHT |