-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sum(i for i in Vector{Int}()) does not return 0 #27766
Comments
Related to #18695 and #18873 (generators don't have an The sum of an empty list is the additive identity ("zero"), but the value of the additive identity may not be |
I guess it should return "zero" (of type |
"For empty collections, it may throw an error or return |
Since |
The root problem here is julia> eltype(i for i in Vector{Int}())
Any Inference works in this particular case: julia> g = (i for i in Vector{Int}())
Base.Generator{Array{Int64,1},getfield(Main, Symbol("##7#8"))}(getfield(Main, Symbol("##7#8"))(), Int64[])
julia> Base.return_types(g.f, Tuple{eltype(g.iter)})
1-element Array{Any,1}:
Int64 but it may be tricky to use this information without making |
Indeed. See also discussion about element type of generators at https://discourse.julialang.org/t/can-eltype-deduce-the-element-type-of-a-generator/6429. |
We do generally make a best effort for empty collections based on inferred return types though. |
The problem here is that we'd need |
Sure but why can't the error only happen in that case? |
Because that would probably make people rely on inference. Imaginary bug report: That does not sound too attractive to me. (Also, figuring all that out probably cost someone a good amount of time.) Making the element type of empty arrays depend on inference already was a measure of last resort. Deciding whether to throw an error or not depending on inference seems way too risky to me. Although I admit it will work most of the time, encouraging people to write code that works only most of the time is questionable. |
That is true in every case were we use |
Yes. Chances are that the element type of an empty collection does not matter at all. Furthermore, the element type for the empty case may only be wider than in the non-empty case, so inserting an element should usually not be trap. An example where you may get a sudden error would be So for the IMO, a much saner approach would be to let |
Maybe
|
Even if the type can be inferred, you may not be able to create an appropriate zero since array types don't carry their sizes with them. What is
? I mean, what is |
Not being able to conjure a zero of some type is a different issue than not knowing what type of zero one wants. That can be a failure of |
To follow up on the lines of thought hinted at by @martinholters and @StefanKarpinski: Perhaps the real problem is the lack of a generic representation of additive identity, which is a well-defined concept. Would it be crazy to have a singleton type to represent this concept in cases where the type is abstract? In relevant operations it could just promote to the appropriate concrete type. |
We already have that: |
The |
We'd need a "strong zero" in the additive sense, and |
The idea of a strong zero seems analogous of |
It seems inconsistent that |
This is something that I often trip over as well. +1 for resolving it. I would be quite comfortable putting much of the burden on the user by prescribing and
|
That's a nice idea, @cortner 👍 |
FYI I created https://github.com/tkf/InitialValues.jl to implement this approach for general But, in this case, I'm +1 for |
#36188 adds |
#36188 is merged so we can use |
Thanks @tkf! Will it make it into 1.5? |
We already had 1.5 feature freeze like a few weeks ago, unfortunately. So, it'll be in 1.6. |
sum(i for i in Vector{Int}())
raises an error instead of returning 0. The sum of an empty list is zero (mathematically). Moreover in this case the return type (Int
) is also clear. Therefore I don't see why this should not return 0 (Int
). Of course the same applies to any number type.See also: https://discourse.julialang.org/t/sum-i-for-i-in-vector-int-raises-error-instead-of-returning-0/1705/8
The text was updated successfully, but these errors were encountered: