Skip to content

accumulate(op, v0, x) fails on arrays of non-numeric types #25506

@chfin

Description

@chfin

The following produces an error:

julia> accumulate((acc, x) -> acc+x[1], 0, [(1,2), (3,4), (5,6)])
ERROR: MethodError: no method matching rcum_promote_type(::##1#2, ::Type{Int64}, ::Type{Tuple{Int64,Int64}})
Closest candidates are:
  rcum_promote_type(::Any, ::Type{T<:Number}) where T<:Number at multidimensional.jl:585
  rcum_promote_type(::Any, ::Type{T}) where T at multidimensional.jl:586
  rcum_promote_type(::Any, ::Type{T}, !Matched::Type{S<:Number}) where {T, S<:Number} at multidimensional.jl:584
  ...
Stacktrace:
 [1] accumulate at ./multidimensional.jl:760 [inlined] (repeats 2 times)
 [2] macro expansion at ./REPL.jl:97 [inlined]
 [3] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73

The same happens for

accumulate((acc, x) -> x, "", ['a', 'b', 'c'])

It seems that accumulate(op, v0, x) tries to guess the type of the output array based on both the type of the initial value and the type of the input array, while it should only consider the first.

Changing this line:

T = rcum_promote_type(op, typeof(v0), eltype(x))

to:

    T = rcum_promote_type(op, typeof(v0))

seems to fix the issue, but I am not sure whether there is a reason to prefer the original version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions