Where is 'findprev' located?

I guess it is from the Base module.

How to find it out?

You can use the command methods(findprev) to see the files involved. There are multiple methods for this function in several files, depending on the input type.

4 Likes

If you are unsure which of the methods from @steffenPL s call is actually called, you can use @which and it tells you which version it dispatches on

julia> @which findprev([false, false, true, false],3)
findprev(A, start)
     @ Base array.jl:2173
6 Likes

And to slightly expand on @kellertuer’s answer: You can also use @edit to directly open the code definition in your favourite editor.

julia> @edit findprev([false, false, true, false],3)
#= editor opens =#
5 Likes

unfortunately, it seems that these work only for some inbuilt functions of Julia. I am using a package written by some people, and neither @which nor @edit work for the functions in the package. I got

ERROR: expression is not a function call, or is too complex for @edit to analyze; break it down to simpler parts if possible. In some cases, you may want to use Meta.@lower.
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:35
 [2] top-level scope
   @ REPL[8]:1

@jiang_ming_zhang can you give a complete example that we can try?

1 Like

As the error message states, I would also think that maybe the arguments of your function call are a bit too complex (maybe with nested function calls?)

Note that the whole @which command works for arbitrary packages and functions since all packages provide their code, so e.g.

julia> using JuliaFormatter
julia> @which format(".")
format(path::AbstractString; options...)
     @ JuliaFormatter ~/.julia/packages/JuliaFormatter/ddD3y/src/JuliaFormatter.jl:425

or also for packages in development mode

julia> using Manifolds
julia> @which Sphere(3)
Sphere(n::Int64)
     @ Manifolds ~/path-to-your-local-repo/src/manifolds/Sphere.jl:55

Or indeed for functions that the user just defined in a current session:

julia> f(x::Int) = 1
f (generic function with 1 method)

julia> f(x::Float64) = 1.0
f (generic function with 2 methods)

julia> @which f(1)
f(x::Int64)
     @ Main REPL[17]:1

julia> @which f(1.0)
f(x::Float64)
     @ Main REPL[18]:1
1 Like