Hacker News new | ask | show | jobs
by pansa 2332 days ago
Julia is built around multiple dispatch - when making a call, the method to execute is chosen based on the run-time types of all of the arguments.

Once this choice has been made, Julia compiles the method, specialised for the exact types of the arguments.

So, the dispatch process and the JIT compiler are linked - both are reliant on type information every time a function is called.

This specialisation is the only way the Julia JIT uses runtime type information. Unlike JavaScript JITs, Julia does not track things like the types of local variables during execution (although it may do some static inference). Therefore type annotations for local variables can improve performance.

1 comments

Mostly spot on, although it's very rare for local variables to require type annotation—type inference can easily figure it out for locals. Unless, of course, they are assigned from an untyped external source the compiler couldn't possibly know. You could, for example read something from a JSON source knowing that it should be an number, annotate it with `json["field"]::Float64` and then the compiler will emit code to check the type and throw an error unless the type is actually a Float64.
Thanks for the reply - I wasn’t sure what Julia did in terms of type inference.

So, if not on local variables, where are explicit types necessary (other than function parameters)?

You actually don't need explicit types in function parameters unless you're trying to control dispatching. Otherwise the compiler will auto-specialize on call. This blog post might be a better explanation on when it's necessary: https://www.stochasticlifestyle.com/type-dispatch-design-pos...