Virtual method dispatch isn’t always a good thing though. It’s a tradeoff and typically you really want one or the other. C++ makes both available for that reason, although C++’s syntax is horrible.
Re:Calculus with Julia
by jakobnissen on Sunday May 19, 2024 @04:26AM
(#40405290)
It's multiple dispatch though, not virtual dispatch. If I understand correctly, virtual dispatch means the method is picked at runtime. In Julia, the compiler is able to pick the correct method at compile time, even when dispatching on all arguments of the function.
This is correct, but it's important to realize that compile time continues during runtime.
Julia's execution model is quite distinctive, and it does take a thorough understanding of how it plays out to get native-code speeds out of it.
`x.plus(y)` dispatches based on (runtime) type of x (polymorphism) (single dispatch), and might call different functions based on the (compile time) type of y (function overloading)
In Julia, it would be `plus(x, y)`, and it can dispatch based on the runtime type of both x and y (or determine the correct method to call at compile time already, if the types of x and y can be determined then).