Hi. I was wondering what is the correct way to pass a function as argument 
preserving the return type information of this function

For instance in the following code:



































*function sinc{T<:Real}(x::T)    return T((x != zero(T) 
)?(sin(pi*x)/(pi*x)):(one(T)))endfunction calcF{T<:Real}(X::Array{T,1}, 
F::Function)    n = length(X); @assert(n > 0)        y = zeros(T, n)    for 
i in eachindex(X)        y[i] = F(X[i])    end    return yendfunction 
calcSinc{T<:Real}(X::Array{T,1})    n = length(X); @assert(n > 0)        y 
= zeros(T, n)    for i in eachindex(X)        y[i] = sinc(X[i])    end    
return yendfunction main{T<:Real}(min::T, max::T, d::T)    X = 
collect(min:d:max)    calcF(X, sinc)    calcSinc(X)    @code_warntype 
calcF(X, sinc)    @time calcF(X, sinc)    @code_warntype calcSinc(X)    
@time calcSinc(X)    YF = calcF(X, sinc)    YS = calcSinc(X)end*

Running this code with julia (versions 0.5+dev or 0.4.2) I got these 
results:






*      #s1 = GenSym(5) # /home/me/scratch/julia_fd/teste.jl, line 10:      
GenSym(2) = 
(F::F)((Base.arrayref)(X::Array{Float64,1},i::Int64)::Float64)::ANY      
(Base.arrayset)(y::Array{Float64,1},(top(typeassert))((Base.convert)(Float64,GenSym(2))::ANY,Float64)::Float64,i::Int64)::Array{Float64,1}
      
5:       unless 
(Base.box)(Base.Bool,(Base.not_int)((Base.box)(Base.Bool,(Base.not_int)(#s1::Int64
 
=== 
(Base.box)(Base.Int,(Base.add_int)((top(getfield))(GenSym(0),:stop)::Int64,1))::Bool))))
 
goto 4      3:   0.080181 seconds (1.88 M allocations: 33.556 MB, 6.41% gc 
time)*

and for the calcSinc function:
   






*   #s1 = GenSym(5) # /home/me/scratch/julia_fd/teste.jl, line 20:      
GenSym(2) = 
(Main.sinc)((Base.arrayref)(X::Array{Float64,1},i::Int64)::Float64)::Float64    
  
(Base.arrayset)(y::Array{Float64,1},GenSym(2),i::Int64)::Array{Float64,1}      
5:       unless 
(Base.box)(Base.Bool,(Base.not_int)((Base.box)(Base.Bool,(Base.not_int)(#s1::Int64
 
=== 
(Base.box)(Base.Int,(Base.add_int)((top(getfield))(GenSym(0),:stop)::Int64,1))::Bool))))
 
goto 4      3:   0.031224 seconds (2 allocations: 4.794 MB)*
Is there a way to prevent the result of function to "evaluate" to ::Any on 
the first case?

thanks

Reply via email to