That is not a very satisfying answer... If Julia's REPL emits errors
for perfectly valid code, something is wrong.
I tried this with Julia 0.4 (development version), and there is no
error. (Good!) However, the type of s is still Array{Any,1} instead of
Array{Int,1}. I appreciate that correcting this may be difficult, but
all the information is right there -- the type of t is known.
If expressions need to be wrapped in functions to make things work,
then maybe the REPL could do that automatically?
Anyway, my main point being: The fact that sum reports an error here
is clearly an error, and seems corrected in the development version.
-erik
On Wed, Nov 19, 2014 at 3:37 AM, Mauro <[email protected]> wrote:
> This is because Julia's type inference is not good in global scope and
> thus the list comprehension returns a Array{Any,1}. Wrap it in a
> function instead:
>
> julia> t = [1:2]
> 2-element Array{Int64,1}:
> 1
> 2
>
> julia> s = [sum(t[1:i]) for i=1:length(t)]
> 2-element Array{Any,1}:
> 1
> 3
>
> julia> zero(eltype(e)) # this is what happens inside var
> ERROR: `convert` has no method matching convert(::Type{MathConst{:e}},
> ::Int64)
> in zero at number.jl:47
>
> julia> f(t) = [sum(t[1:i]) for i=1:length(t)]
> f (generic function with 1 method)
>
> julia> f(t)
> 2-element Array{Int64,1}:
> 1
> 3
>
> julia> s =f(t)
> 2-element Array{Int64,1}:
> 1
> 3
>
> julia> zero(eltype(s))
> 0
>
> Generally it is good to put all your stuff into functions so Julia can
> do its type inference. Better performance and sometime, as in your
> case, no errors.
>
> On Tue, 2014-11-18 at 23:27, witek gawlowski <[email protected]>
> wrote:
>> Hello,
>> some basic question although i couldn't find an answer easily:
>>
>> if i run
>> using StatsBase
>> t = [1:10]
>> s = [sum(t[1:i]) for i=1:10]
>> var(s)
>>
>> i get an error: `zero` has no method matching zero(::Type{Any})
>>
>> I see t is of [a, b, c] type and s is of {a, b, c} type. Can you give me
>> the topics to search for?
>> also if s is representing a list why do i have to convert it to something
>> else (i guess) to calculate its variance?
>>
>> Regards
>
--
Erik Schnetter <[email protected]>
http://www.perimeterinstitute.ca/personal/eschnetter/