List comprehension `no method matching zero` issue

It looks like the element type of the comprehension is inferred as Any, and, from the stacktrace, it looks like it is empty, so Julia errors when you call sum on it:

julia> x = Any[];

julia> sum(x)
ERROR: MethodError: no method matching zero(::Type{Any})

If you know what element type you expect (e.g. Int), then you can use a typed comprehension: Int[...] instead, but perhaps there is a bug in your code since I am guessing you didn’t expect it to be empty in the first place.

2 Likes