3 ms·
I think you may have some misconceptions. Julia is not compiled ahead of time. A function does not spring into existence until the definition of the function is
by socialdemocrat 6y ago
I think you may have some misconceptions. Julia is not compiled ahead of time. A function does not spring into existence until the definition of the function is executed. Same applies to a struct definition.
In a statically typed language you might write a function that takes an integer like this `foo(x::Int)` but in Julia it is completely possible to replace the `Int` with an expression that returns a type object. Meaning you actually run code at runtime to figure out what the defined function should actually operate on.
So there is nothing known at compile time, because everything happens at runtime. However when a function is called for the first time, then the JIT compiler will compile it and store the result. But it will store different compiled versions of the same function depending on the type of the argument it was called with. If you call the same function later with the same type of argument, it will reuse a previously compiled version.
If this is all very unclear. You could look at an article I wrote contrasting static typing with how typing works in Julia: https://medium.com/@Jernfrost/types-in-c-c-and-julia-ce0fcbe0dec6 https://medium.com/@Jernfrost/types-in-c-c-and-julia-ce0fcbe...
- ChrisFoster 6y ago> A function does not spring into existence until the definition of the function is executed. Methods and their type signatures do exist within the runtime as soon as the function expression is evaluated. To simplify the example from your article julia> a = 20 20 julia> foo(x::(a < 10 ? Int : Float64)) = "thing" foo (generic function with 1 method) julia> methods(foo) # 1 method for generic function "foo": [1] foo(x::Float64) in Main at REPL[2]:1 Here you see that one method of the function foo has been defined, and the code in the type signature has already run, resulting in a method which takes x::Float64. After this point, changing the value of `a` will not change anything about foo.