Le dimanche 14 septembre 2014 à 06:25 -0700, Laurent Journot a écrit :
> I am a biologist with no knowledge in programing. I recently started
> to write small bits of code to analyze gene co-expression networks. By
> the way, congratulations to those who developped Julia and the many
> available packages. It's really great and fairly easy to make simple
> things in a matter of days with the intuitive Julia syntax.
> 
> I am not familar with type management in programing languages but some
> behaviors are puzzling for a tenderfoot like myself. Here is a piece
> of stupid code just to illustrate my question (I'm using JuliaStudio):
> 
> julia> A=[1,2]
> 
> 2-element Array{Int64,1}:
>  1
>  2
> 
> 
> julia> typeof([A[1]])
> 
> Array{Int64,1}
> 
> julia> typeof([A[i] for i=1])
> Array{Any,1}
> 
> Of course int() is doing the job.
> 
> julia> typeof(int([A[i] for i=1]))
> Array{Int64,1}
> 
> Yet, I find this behavior strange and using int(), float()... makes the code 
> unnecessarily heavy.
> I looked through the posts on julia-users but could not find information 
> about how to avoid type change during comprehension. Any advice ?

This issue usually happens when using comprehensions at the top-level,
as opposed to putting them in functions. AFAIK, the problem is that
Julia is not (yet?) able to detect that the code in the comprehension
does not change the type of A. Type inference may get better in the
future.

You can declare A as being constant to work around this:

julia> const A = [1,2]
2-element Array{Int64,1}:
 1
 2

julia> typeof([A[i] for i=1])
Array{Int64,1}


Also, instead of int([A[i] for i=1]), you can write Int[A[i] for i=1],
to avoid converting the array after it has been created (and it's also
slightly less typing).


Regards

Reply via email to