Note that this is only an issue when you do it from the REPL; if you define a
function, it works as expected:
julia> acomp(a) = [a[i] for i = 1:length(a)]
julia> c = acomp(a)
3-element Array{Int64,1}:
1
2
3
The issue is that when a function is compiled, there's more type analysis that
can be done (in particular, it can trust that the type of the elements of a is
constant over the duration of the function execution).
--Tim
On Friday, February 14, 2014 11:48:58 PM Jacob Quinn wrote:
> Try the following:
>
> b = [a[i]::Int64 for i=1:3]
>
> This is hinting the type, see
> http://docs.julialang.org/en/latest/manual/performance-tips/#annotate-values
> -taken-from-untyped-locations
>
> -Jacob
>
> On Fri, Feb 14, 2014 at 12:04 PM, Nico <[email protected]> wrote:
> > When I build an array using a comprehension as follows:
> > a = [i for i=1:3]
> >
> > its type is 3-element Array{Int64,1}.
> >
> > However, when I build a second array as:
> > b = [a[i] for i=1:3]
> >
> > its type is 3-element Array{Any,1} instead of Array{Int64,1}.
> >
> >
> > How can I build b so that it preserves the type of a?