The order of multiple dispatch

Hi, I am learning Julialang on Mac and Windwos.
I have a question about the order of multiple dispatch.

function dfunc(args::Int64)
println(“Int64”)
end

function dfunc(args::Any)
println(“Any”)
end

julia> dfunc(255)
Int64

julia> dfunc(“Test”)
Any

Yes,These functions work as expected.
However, is the order in which they are evaluated clearly defined?
My question is whether we don’t need to worry about Int64 being evaluated by Any.

I look forward to your kind advice.

The order doesn’t matter. The method with the most specific signature that matches the argument types will be used.

9 Likes

Dear kristoffer,

Thank you very much your advice.
I understood that multiple dispatch is managed by signature.

If you know the location of the relevant Julia source code, could you let me know?

Dispatch is handled by a pretty low-level part of the Julia implementation; reading through the dispatch code is not something likely to be helpful for someone just starting to learn Julia.

(There are plenty of other parts of Julia that are implemented in Julia with very readable code that it is informative to read. e.g. the complex-number implementation.)

6 Likes

Here is the relevant section of the manual: https://docs.julialang.org/en/v1/manual/methods/#Defining-Methods-1

1 Like

Dear Steven,

Thank you a lot of advice.

I try to read suggestions source code.

Dear klaff,

Thank you very much for your message.
I try to understand the docs.

Dear Steven,

I found “defining and adding methods” in method.c.
I will try to understand signeture system from here.

Thanks a lot.

1 Like

Are you planning on learning Julia by reading the C source? That’s impressive and/or borderline crazy😁

I’ve never read any of the C source, and never will. If you are going on a real deep dive, I’m sure it will be a fascinating read, but are you confident that this is the right order? Why not start with the documentation instead?

https://docs.julialang.org/en/v1/manual/methods/#Methods-1

4 Likes

Dear DNF,

Thank you for your advices.

Creating languages for myself and playing bass guitar are two of my hobbies.
C is an easy language for me to understand, as I’ve been using it for work since the 8-bit days.
I’ll make an effort to read a lot of documentation as you suggest.

Thanks.

11 Likes

I :heart: this. If you have any questions about the source code, feel free to ask on the JuliaLang Slack #internals channel, although you can also ask here but Slack may be more immediate and interactive.

4 Likes

Nice! If you’re interested in how method specificity is defined, look at the functions with morespecific in their names in src/subtype.c.

3 Likes

Dear Karpinski,

Thank you very much for good information.
I will check Slack.

Thanks.

1 Like

Dear bezanson,

I will check src/subtype.c

Thanks a lot.

1 Like