Consider the findfirst
function. I can see which methods are defined for findfirst
by using methods
:
julia> methods(findfirst)
# 9 methods for generic function "findfirst":
[1] findfirst(A::Union{AbstractString, AbstractArray}) in Base at array.jl:1672
[2] findfirst(p::Union{Base.Fix2{typeof(==),T}, Base.Fix2{typeof(isequal),T}}, r::StepRange{T,S}) where {T, S} in Base at array.jl:1758
[3] findfirst(pred::Base.Fix2{#s66,#s65} where #s65<:Union{Int8, UInt8} where #s66<:Union{typeof(==), typeof(isequal)}, a::Union{Array{Int8,1}, Array{UInt8,1}}) in Base at strings/search.jl:22
[4] findfirst(testf::Function, A::Union{AbstractString, AbstractArray}) in Base at array.jl:1754
[5] findfirst(testf::Function, A) in Base at array.jl:1747
[6] findfirst(pattern::AbstractString, string::AbstractString) in Base at strings/search.jl:104
[7] findfirst(ch::AbstractChar, string::AbstractString) in Base at strings/search.jl:124
[8] findfirst(r::Regex, s::AbstractString) in Base at regex.jl:327
[9] findfirst(A) in Base at array.jl:1663
Now suppose I'd like to figure out which of those methods gets dispatched when I call findfirst(iseven, 1:4)
. How do I do that?
Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time.
So the dispatch function also has access to reducer - our reducer function (no matter what we called it) that we passed in. That means that the dispatch function can invoke the reducer function.
In Eclipse, one can right click on a method and choose "open call hierarchy" and eclipse shows all the methods that call the selected method and all the methods that are called from the selected method (callers and callees of the selected method). We can programtically find callers and callees and I will explain how I have done it.
The dispatch function is in the same scope as the current state of the app. So that means that inside the dispatch function, we have access to an object called currentState that is the current state in our app.
You can use the @which
macro:
julia> @which findfirst(iseven, 1:4)
findfirst(testf::Function, A::Union{AbstractString, AbstractArray}) in Base at array.jl:1754
Another example:
julia> @which length(1:7)
length(r::AbstractUnitRange{T}) where T<:Union{Int128, Int64} in Base at range.jl:542
julia> @which length("hello world")
length(s::String) in Base at strings/string.jl:259
Edit:
As @giordano points out in the comments, you can use
@less findfirst(iseven, 1:4)
or
@edit findfirst(iseven, 1:4)
if you want to see the source code for the method that would get dispatched. @less
displays the source code using the system pager. @edit
will open the file containing the method source code in a text editor. You can control which editor gets opened by setting the JULIA_EDITOR
environment variable. For example, you can set your editor to vim by calling
ENV["JULIA_EDITOR"] = "vim"
either at the REPL or in your startup file, ~/.julia/config/startup.jl
. Alternatively, you can add export JULIA_EDITOR=vim
to your .bashrc
or .bash_profile
file on linux/macos.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With