-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Open
Labels
foldsum, maximum, reduce, foldl, etc.sum, maximum, reduce, foldl, etc.
Description
See discussion https://discourse.julialang.org/t/interface-for-number/2723/5
I recently ran into an issue overriding zero
for my own Number
:
julia> immutable Infinity <: Number end
julia> Base.zero(::Infinity) = 0
julia> import Base: +
julia> +(::Infinity,::Int) = Infinity()
+ (generic function with 181 methods)
julia> reduce(+,[Infinity(),Infinity()])
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Infinity
This may have arisen from a call to the constructor Infinity(...),
since type constructors fall back to convert methods.
Stacktrace:
[1] _mapreduce(::Base.#identity, ::Base.#+, ::IndexLinear, ::Array{Infinity,1}) at ./reduce.jl:260
[2] reduce(::Function, ::Array{Infinity,1}) at ./reduce.jl:321
The issue appears to be that zero{T<:Number}(::T)
needs is expected to be convertible to T
.
Is this a bug? Otherwise, this should be documented somewhere (e.g., a description of the interface for Number
)
mbauman
Metadata
Metadata
Assignees
Labels
foldsum, maximum, reduce, foldl, etc.sum, maximum, reduce, foldl, etc.