[Feature] why not a `functionswith` method?

The idea is to print a simpler and shorter list than methodswith. Since we have methodswith and methods, and the function name of a method is obtained by accessing it name field, for me it seems that functionswith is missing somehow. I think it would be useful.

1 Like

While not exactly what you are looking for, there is apropos, which searches the whole docstring for a specified string:

help?> apropos
search: apropos hasproperty

  apropos(string)

  Search through all documentation for a string, ignoring case.

julia> apropos("filter")
Base.:!
Base.FilteringRF
Base.filter
Base.Generator
Base.filter!
Base.Iterators.filter
Base.CoreLogging.AbstractLogger
Base.CoreLogging.min_enabled_level
Base.CoreLogging.LogLevel
Base.CoreLogging.handle_message
Base.Math._approx_cbrt
Base.Math._ldexp_exp
Base.StackTraces.from
LibGit2.diff_files
LibGit2.CheckoutOptions
Logging
Logging.Logging
Logging.ConsoleLogger
Test.@test_logs

Does that also cover your needs?

1 Like

I mean to use functionswith as

julia> functionswith(Int64) 
82-element Array{Symbol,1}:
 :/
 :rem
 :&
 :*
 :+
 :-
 :<
 :<<       
 :<=
 :(==)
 :>>
 :>>>
 ⋮
 :middle
 :clear!
 :serialize
 :nlargest
 :nsmallest
 :reset!
 :update!
 :tofirst
 :tolast
 :tonext
 :toprev
 :connect

Indeed I have something implemented

functionswith(X) = unique(map((x)->getfield(x,:name), methodswith(X)))

You should use the function rather than the name. The name has little significance.

Thanks Yuyichao, but I don’t know how to do that… sorry

Something like this might do the trick:

julia> functionswith(T) = unique(fieldtype(method.sig, 1).instance for method in methodswith(T))
functionswith (generic function with 1 method)

julia> functionswith(Int)
67-element Array{Function,1}:
 middle (generic function with 6 methods)
 clear! (generic function with 7 methods)
 serialize (generic function with 53 methods)
 rem (generic function with 139 methods)
 & (generic function with 16 methods)
...

There’s a few corner cases where this will error but maybe it’s a useful starting point.

FWIW, the corner cases are a good illustration why it’s hard to create something like your request. Consider the following definition:

julia> (::Val{N})(x::NTuple{N}) where N = N

julia> Val(3)((4,5,6))
3

This gives us a method that’s not associated to a well-defined function, because there’s a where clause over both the function and the argument:

julia> @which Val(3)((4,5,6))
(::Val{N})(x::Tuple{Vararg{T,N}} where T) where N in Main at REPL[12]:1

julia> (@which Val(3)((4,5,6))).sig
Tuple{Val{N},Tuple{Vararg{T,N}} where T} where N

This illustrates why methodswith is in Base but not functionswith. Does that help?

3 Likes

Sure! I think this could be used in somoe IDEs to suggest you functions based on a variable you type… for instance this happens in single dispatched languajes and helps a lot for the discoverability of the functionalities of different types. I just took a screenshot of a JavaScript console suggesting you some methods for x = 1.
image

I think functionswith is not a bizzarre idea, and might be in InteractiveUtils. If that’s not the case, it was nice to discusse this with all you guys, and I’m very thankful.

Thanks

PS, I’ll cite this in a Juno.jl feature request issue

1 Like