Function Signatures in julia

I am interested in understanding the actual difference, if any, between the following two function definitions

function foo(n::Integer) println("hello") end function foo{T<:Integer}(n::T) println("hello") end 

As far as I understand, the second form starts a new compilation every time a function is called for a new type T, but what actually happens in the first case? Is there a performance effect associated with the first form?

thanks

+5
source share
1 answer

Function argument parameters are for the value of the method, not for performance. From the docs http://docs.julialang.org/en/latest/manual/style-guide/#avoid-writing-overly-specific-types

The key to the implementation is that there is no performance limit for determining only the general addone (x) = x + one (x), since Julia will automatically compile specialized versions as needed. For example, the first time addone (12) is called, Julia will automatically compile the specialized addone function for x :: Int arguments with the call to one () replaced by its built-in value 1. Therefore, the first three addone definitions above are completely redundant.

CHANGE comment address:

The difference between the two signatures is really obvious only if there is more than one argument

Let's consider two functions:

 julia> function foo(n::Integer, m::Integer) println(typeof(n), typeof(m)) end julia> function foo{T<:Integer}(n::T, m::T) println("Parameterized: ", typeof(n), typeof(m)) end 

In the first function, n and m must be integers, but they must not be the same subtype of the integer. In the second function, both m and n must be of the same subtype

 julia> foo(1, 2) Parameterized: Int64Int64 julia> foo(1, Int32(1)) Int64Int32 
+5
source

All Articles