Function declaration order?

I am a bit confused about the function declaration order.

For example, if I write following module:

module A
function callzeros()
    zeros(rand(5,5))
end

function callzeros2()
    myzeros()
end
callzeros2()
function myzeros()
   print("myzero")   
end

function Base.zeros(like::Array{T}) where T<:Any
    return zeros(size(like))
end

callzeros()
end

Then is the function callzeros going to recognized the my override? In general, does the function declaration order matter? If it does, is there any way get around this requirement like putting a function declaration in the header file in C++?

1 Like

Declaration order doesn’t matter unless you’re overwriting a method (i.e. the exact same method name and argument types).

2 Likes

Can I clarify a little bit more: if I am overloading the same function with a slightly different type signatures in different submodules of a module, the order does not matter?

I am still a bit confused. In the example I gave, this module does not compile because myzeros is not recognized by callzeros2()

Exactly.

However, when you run code, then only the already-defined methods are available. Normally, you wouldn’t run code in a module (or you run it at the very end), so order does not matter.

2 Likes

Hmmm. Interesting… So the rule of thumb is that as long as I am doiing everything inside function definition(within the module), I don’t need to worry about the declaration order, right?

2 Likes

it’s called multi-dispatch (as it’s different from C++), more here: https://docs.julialang.org/en/v1/manual/methods/index.html

also: https://youtu.be/kc9HwsxE1OY?t=495

this is fabulous luv…even with video! I think I misunderstood the multidispatch until now!

Exactly. The method is only “looked up” when it’s actually called. Inside definitions you can have whatever you want.

thanks,luv.