-
-
Couldn't load subscription status.
- Fork 5.7k
Closed
Labels
arrays[a, r, r, a, y, s][a, r, r, a, y, s]
Description
To compute the eltype of the array storing the result, reducedim does
Line 119 in bce5bbe
| Tr = typeof(z) == typeof(x) && !isbits(T) ? T : typeof(z) |
I don't understand the reasoning behind the condition
typeof(z) == typeof(x) && !isbits(T). Why not just do Tr = typeof(z) ?
Here is a MWE:
struct Variable
name::Symbol
end
struct AffExpr
vars::Vector{Variable}
end
Base.zero(::Union{Variable, Type{Variable}, AffExpr}) = AffExpr(Variable[])
Base.:+(v::Variable, w::Variable) = AffExpr([v, w])
Base.:+(aff::AffExpr, v::Variable) = AffExpr([aff.vars; v])
Base.:+(aff1::AffExpr, aff2::AffExpr) = AffExpr([aff1.vars; aff2.vars])The reduction fails because Tr is Variable instead of AffExpr. typeof(z) and typeof(x) are AffExpr and isbits(Variable) is false.
julia> sum([Variable(:x), Variable(:y)], 1)
ERROR: MethodError: Cannot `convert` an object of type AffExpr to an object of type Variable
This may have arisen from a call to the constructor Variable(...),
since type constructors fall back to convert methods.
Stacktrace:
[1] fill!(::Array{Variable,1}, ::AffExpr) at ./multidimensional.jl:841
[2] reducedim_initarray(::Array{Variable,1}, ::Int64, ::AffExpr, ::Type{Variable}) at ./reducedim.jl:73
[3] _reducedim_init(::Base.#identity, ::Base.#+, ::Base.#zero, ::Base.#sum, ::Array{Variable,1}, ::Int64) at ./reducedim.jl:102
[4] reducedim_init(::Function, ::Base.#+, ::Array{Variable,1}, ::Int64) at ./reducedim.jl:87
[5] mapreducedim(::Function, ::Function, ::Array{Variable,1}, ::Int64) at ./reducedim.jl:242
[6] sum(::Array{Variable,1}, ::Int64) at ./reducedim.jl:585
[7] macro expansion at ./REPL.jl:97 [inlined]
[8] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73Metadata
Metadata
Assignees
Labels
arrays[a, r, r, a, y, s][a, r, r, a, y, s]