Hi toguldur,
It's because, currently, julia has a hard time optimizing global variables,
which includes all variables defined on the REPL. This is a known issue
(and comes up on this list frequently).
The current solution is to wrap everything in a function:
julia> fn(x::Int) = (b = 3; [i + 2.5 - b for i=1:x])
fn (generic function with 1 method)
julia> fn(10)
10-element Array{Float64,1}:
0.5
1.5
2.5
3.5
4.5
5.5
6.5
7.5
8.5
9.5
Check out
http://julia.readthedocs.org/en/latest/manual/performance-tips/#avoid-global-variables
for more information.
Cheers,
Kevin
On Sun, Jan 11, 2015 at 7:33 PM, <[email protected]> wrote:
> A new user here. I am wondering why a non-predefined array get a type
> {Any}? also why some mathematical functions don't work on it?
>
> if I define an array "a" as:
> a=[i for i=1:10]
> then it is automatically {Int64}. If change it to:
> a=[i+2.5 for i=1:10]
> then it is automatically {Float64} as expected. However, when I define an
> integer, say: b=3 and use it in the array generation:
> a=[i+2.5-b for i=1:10]
> then it becomes {Any}. Why it is still not Float64?
>
> I came to this as I was trying to take a log of an array, and it was
> complaining that it can't operate on type {Any}.
>
> thanks,
>