Working with v0.6.2, official release on linux, I was constructing a composite type that wraps Array
. Then I ran into trouble when I tried to introduce broadcasting to the new type.
Here is my minimal working example:
julia> struct Vec
data::Array{Float32, 1}
end
julia> v = Vec(Float32[1, 2, 3])
Vec(Float32[1.0, 2.0, 3.0])
julia> Base.Broadcast._containertype(::Type{Vec}) = Vec
julia> Base.Broadcast.promote_containertype(::Type{Vec}, ::Type{Vec}) = Vec
julia> Base.Broadcast.promote_containertype(::Type{Vec}, _) = Vec
julia> Base.Broadcast.promote_containertype(_, ::Type{Vec}) = Vec
julia> Base.Broadcast.broadcast_c(::typeof(^), ::Type{Vec}, x::Number, y::Vec) = Vec(x .* y)
julia> 2.*v
ERROR: MethodError: no method matching broadcast_c(::##1#2, ::Type{Vec}, ::Vec)
Closest candidates are:
broadcast_c(::Any, ::Type{Array}, ::Any, ::Any...) at broadcast.jl:312
broadcast_c(::Any, ::Type{Nullable}, ::Any...) at broadcast.jl:324
broadcast_c(::Any, ::Type{Any}, ::Any...) at broadcast.jl:337
...
Stacktrace:
[1] broadcast(::Function, ::Vec) at ./broadcast.jl:455
I notice that broadcast
works:
julia> broadcast(*, 2, v)
Vec(Float32[2.0, 4.0, 6.0])
I understand that this is not a brand new question.
I found some related posts:
The second one seems exactly the same as mine, and the reply indeed suggested the solution offered in the first one. However, as can be seen in the example above, it didn’t work apparently.
Is there anything I am missing?