Also in Julia there is static and dynamic dispatch. Static dispatch happens when inference can figure out the concrete type. For example (with the definitions from my previous post):
function static_dispatch()
c1 = C1()
c2 = C2()
foo(c1, c1)
foo(c1, c2)
foo(c2, c1)
foo(c2, c2)
nothing
end
@code_warntype static_dispatch()
prints
Variables:
#self#::#static_dispatch
c1::C1
c2::C2
Body:
begin # line 23: # line 24:
$(QuoteNode(3)) # line 25:
$(QuoteNode(2)) # line 26:
$(QuoteNode(1)) # line 27:
$(QuoteNode(1)) # line 28:
return Main.nothing
end::Void
(In this case, the method invocations have been replaced with their constant return values, so this is not the best example).
Static dispatch is the same in C++:
C1 c1 = C1();
C2 c2 = C2();
std::cout << c1.foo(&c1) << std::endl;
std::cout << c1.foo(&c2) << std::endl;
std::cout << c2.foo(&c1) << std::endl;
std::cout << c2.foo(&c2) << std::endl;
which prints the same values
3
2
1
1
Thus, multiple dispatch in Julia is only special at runtime, i.e., when the type cannot be statically inferred (there might be more subtle differences in static dispatch, too).