Can methodswith() return everything?

proposal

#1

I love the methodswith() function as it lets me quickly find out the functions that take a particular type directly in the REPL without having to look up the type’s documentation on the web.

However, I found it to be too restrictive and it’s missing something. For example,

julia> methodswith(Dict)
29-element Array{Method,1}:
 convert(::Type{Dict{K,V}}, d::Dict{K,V}) where {K, V} in Base at dict.jl:208                                                                          
 copy(d::Dict) in Base at dict.jl:134                                                                                                                  
 delete!(h::Dict, key) in Base at dict.jl:558                                                                                                          
 done(t::Dict, i) in Base at dict.jl:578                                                                                                               
 empty!(h::Dict{K,V}) where {K, V} in Base at dict.jl:304                                                                                              
 get(h::Dict{K,V}, key, default) where {K, V} in Base at dict.jl:478                                                                                   
 get(default::Union{Function, Type}, h::Dict{K,V}, key) where {K, V} in Base at dict.jl:483                                                            
 get!(h::Dict{K,V}, key0, default) where {K, V} in Base at dict.jl:434                                                                                 
 get!(default::Union{Function, Type}, h::Dict{K,V}, key::K) where {K, V} in Base at dict.jl:444                                                        
 get!(default::Union{Function, Type}, h::Dict{K,V}, key0) where {K, V} in Base at dict.jl:436                                                          
 getindex(h::Dict{K,V}, key) where {K, V} in Base at dict.jl:473                                                                                       
 getkey(h::Dict{K,V}, key, default) where {K, V} in Base at dict.jl:527                                                                                
 haskey(h::Dict, key) in Base at dict.jl:505                                                                                                           
 isempty(t::Dict) in Base at dict.jl:581                                                                                                               
 launch(manager::Base.Distributed.SSHManager, params::Dict, launched::Array, launch_ntfy::Condition) in Base.Distributed at distributed/managers.jl:121
 launch(manager::Base.Distributed.LocalManager, params::Dict, launched::Array, c::Condition) in Base.Distributed at distributed/managers.jl:317        
 length(t::Dict) in Base at dict.jl:582                                                                                                                
 next(t::Dict{K,V}, i) where {K, V} in Base at dict.jl:579                                                                                             
 pop!(h::Dict, key) in Base at dict.jl:538                                                                                                             
 pop!(h::Dict, key, default) in Base at dict.jl:543                                                                                                    
 rand(r::AbstractRNG, t::Dict) in Base.Random at random.jl:374                                                                                         
 rand(t::Dict) in Base.Random at random.jl:381                                                                                                         
 serialize(s::AbstractSerializer, d::Dict) in Base.Serializer at serialize.jl:337                                                                      
 setindex!(h::Dict{K,V}, v0, key::K) where {K, V} in Base at dict.jl:420                                                                               
 setindex!(h::Dict{K,V}, v0, key0) where {K, V} in Base at dict.jl:412                                                                                 
 similar(d::Dict{K,V}) where {K, V} in Base at dict.jl:192                                                                                             
 similar(d::Dict, ::Type{Pair{K,V}}) where {K, V} in Base at dict.jl:193                                                                               
 sizehint!(d::Dict, newsz) in Base at dict.jl:274                                                                                                      
 start(t::Dict) in Base at dict.jl:574                                                                                                                 

In this case, keys() function isn’t returned. As it turns out, keys take the Associative type as its argument rather than Dict. It’s object-oriented, stupid! :grin:

help?> keys
search: keys keytype KeyError haskey getkey WeakKeyDict takebuf_array

  keys(a::Associative)

  Return an iterator over all keys in a collection. collect(keys(a)) returns an array of keys. Since the keys are stored
  internally in a hash table, the order in which they are returned may vary. But keys(a) and values(a) both iterate a and return
  the elements in the same order.

  julia> a = Dict('a'=>2, 'b'=>3)
  Dict{Char,Int64} with 2 entries:
    'b' => 3
    'a' => 2
  
  julia> collect(keys(a))
  2-element Array{Char,1}:
   'b'
   'a'

I think it would be best if methodswith can return all functions that satisfy the contract. It’s a great utility!


#2

I think you are looking for methodswith(Dict, true) which will show all methods that are defined including for the supertypes of T (except Any).

(Also see the documentation for methodswith)


#3

Haha… Always learning something new :slight_smile:
Thanks, @vchuravy