Convenient ways to finding methods

Coming from the object-oriented programming background, I missed the ease of looking up which methods can be applied to an object. I understand the beauty of multiple dispatch but I don’t know of a good way to figure out all the methods that are relevant to me.

Is there any way to:

  1. List all methods that returns a specific type
  2. List all methods that take a specific type in any of the arguments.

Ideally, the type hierarchy is respected so looking for methods that return Integer should return methods that return any type of Integers, as in:

Integer
    BigInt
    Bool
    Signed
        Int128
        Int16
        Int32
        Int64
        Int8
    Unsigned
        UInt128
        UInt16
        UInt32
        UInt64
        UInt8

There’s methodswith:

julia> methodswith(Float64)

julia> methodswith(Float64, true)   # gives more results
7 Likes

Nice utility! It solves #2 but not #1, however. Maybe it’s somewhat difficult to know the return type without having to precompile?

I guess you can’t do #1 in object-oriented languages either?
In any case, what would that be useful for?

Good question. Let’s try an example.

Let’s say I found a wonderful method that takes IOBuffer as an argument. So I need to create an IOBuffer in some way but don’t know how.

The purpose of these convenient functions is to quickly look things up without referring back to documentations. In fact, it would be even nicer if there’s a “search engine” that can quickly find things, not just methods but also doc strings. I know I can use Google but it’s nice to cut all the noise away and focus on Julia and my environment (my version, my installed packages, etc.)

Julia objects are created using their constructor i.e., IOBuffer() creates an IOBuffer so there is no need to look it up since it has the same name as the object itself.

apropos("IOBuffer")

3 Likes

It’s possible to list all methods and check their return type by running the type inference (Base.code_typed) but that’s a bit slow and there’s lot of methods in Julia so it’s not very realistic to use this approach as a quick tool to find methods.

For 2. it’s possible get all methods that match a type signature (e.g. Int in second position (Any, Int, Any)), that’s relatively easy and fast