Le lundi 23 mars 2015 à 04:39 -0700, Uliano Guerrini a écrit :
> The doc says that "The resulting array type is inferred from the
> expression" so I can't understand why
>
>
>
> k=[-1.0,0.0,2.0]
> A=[k[r]^(c-1) for r=1:3,c=1:3]
>
>
> gives me
>
>
> 3x3 Array{Any,2}:
> 1.0 -1.0 1.0
> 1.0 0.0 0.0
> 1.0 2.0 4.0
>
> sure, if I use
>
>
> A=Float64[k[c]^r for c=1:3,r=0:2]
>
>
>
> I get the expected
>
> 3x3 Array{Float64,2}:
> 1.0 -1.0 1.0
> 1.0 0.0 0.0
> 1.0 2.0 4.0
>
> What is in the expression a Float64 to an Int (I suppose) that gives Any as
> inferred type?
That's because type inference has a hard time checking whether the type
of k remains the same in the global scope. You can declare k as const,
or wrap the code in a function.
Regards