@@ -165,27 +165,61 @@ function showerror(io::IO, ex::MethodError)
165
165
print (io, " since type constructors fall back to convert methods in julia v0.4." )
166
166
end
167
167
168
- # Display up to three closest candidates
168
+ show_method_candidates (io, ex)
169
+ end
170
+
171
+ function show_method_candidates (io:: IO , ex:: MethodError )
172
+ # Displays the closest candidates of the given function by looping over the
173
+ # functions methods and counting the number of matching arguments.
169
174
lines = Array ((IOBuffer, Int), 0 )
170
175
for method in methods (ex. f)
171
- n = length (ex. args)
172
- if n != length (method. sig)
173
- continue
174
- end
175
176
buf = IOBuffer ()
176
- print (buf, " $(ex. f. env. name) (" )
177
- first = true
177
+ print (buf, " $(ex. f. env. name) " )
178
178
right_matches = 0
179
- for (arg, sigtype) in Zip2 {Any,Any} (ex. args, method. sig)
180
- if first
181
- first = false
179
+ tv = method. tvars
180
+ if ! isa (tv,Tuple)
181
+ tv = (tv,)
182
+ end
183
+ if ! isempty (tv)
184
+ show_delim_array (buf, tv, ' {' , ' ,' , ' }' , false )
185
+ end
186
+ print (buf, " (" )
187
+ t_i = Any[typeof (ex. args)... ]
188
+ right_matches = 0
189
+ for i = 1 : min (length (t_i), length (method. sig))
190
+ i != 1 && print (buf, " , " )
191
+ # If isvarargtype then it checks wether the rest of the input arguements matches
192
+ # the varargtype
193
+ j = Base. isvarargtype (method. sig[i]) ? length (t_i) : i
194
+ # checks if the type of arg 1:i of the input intersects with the current method
195
+ t_in = typeintersect (method. sig[1 : i], tuple (t_i[1 : j]. .. ))
196
+ if t_in == None
197
+ # If there is no typeintersect then the type signature from the method is
198
+ # inserted in t_i this insures if the type at the next i matches the type
199
+ # signature then there will be a type intersect
200
+ t_i[i] = method. sig[i]
201
+ Base. with_output_color (:red , buf) do buf
202
+ print (buf, " ::$(method. sig[i]) " )
203
+ end
182
204
else
183
- print (buf, " , " )
205
+ right_matches += j== i ? 1 : 0
206
+ print (buf, " ::$(method. sig[i]) " )
184
207
end
185
- if typeof (arg) <: sigtype
186
- right_matches += 1
187
- print (buf, " ::$(sigtype) " )
188
- else
208
+ end
209
+ if length (t_i) > length (method. sig) && Base. isvarargtype (method. sig[end ])
210
+ # It insures that methods like f(a::AbstractString...) gets the correct
211
+ # number of right_matches
212
+ for t in typeof (ex. args)[length (method. sig): end ]
213
+ if t <: method.sig [end ]. parameters[1 ]
214
+ right_matches += 1
215
+ end
216
+ end
217
+ end
218
+ if length (t_i) < length (method. sig)
219
+ # If the methods args is longer than input then the method
220
+ # arguments is printed as not a match
221
+ for sigtype in method. sig[length (t_i)+ 1 : end ]
222
+ print (buf, " , " )
189
223
Base. with_output_color (:red , buf) do buf
190
224
print (buf, " ::$(sigtype) " )
191
225
end
@@ -196,7 +230,7 @@ function showerror(io::IO, ex::MethodError)
196
230
push! (lines, (buf, right_matches))
197
231
end
198
232
end
199
- if length (lines) != 0
233
+ if length (lines) != 0 # Display up to three closest candidates
200
234
Base. with_output_color (:normal , io) do io
201
235
println (io, " \n Closest candidates are:" )
202
236
sort! (lines, by = x -> - x[2 ])
0 commit comments