I would like to redefine a single broadcast rule relating to the coalesce.
broadcast.
The reason is that the ShiftedArray
type can return missing
or nothing
for out-of-bound shifted access. If the user uses coalesce.(myshiftedarray)
elementwise on such an array, it would be nice if the array itself changed its type to also coalesce
the default return type.
Yet I am struggeling with the broadcasting system to allow for such an effect. The problem is that the coalesce
function is already to fine-grained and does not see the array. Ideally one would like a coarsegrained broadcasting rule which changes the type, which is anyway hidden in my BroadcastStyle ShiftedArrayStyle
. But the problem here is that the broadcasting rules do not seem to contain any information about which function is broadcasted.
So I think one somehow needs to hook directly into the broadcasting by overloading broadcasted
and check if the function bc.f
is equal to coalesce
? But then this also would not yield the correct propagation of ShiftedArrayStyle
. Any ideas?
The Broadcasted
object wraps the function, so perhaps you may dispatch on its type?
julia> B = Broadcast.broadcasted(+, ones(1));
julia> B.f
+ (generic function with 207 methods)
julia> typeof(B)
Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(+), Tuple{Vector{Float64}}}
Yup. This seems to work:
function Base.broadcasted(::typeof(coalesce), a::ShiftedArray, b)
ac = ShiftedArray(a.parent, shifts(a), default=coalesce(default(a), b))
return invoke(Base.broadcasted, Tuple{typeof(coalesce), AbstractArray, typeof(b)}, coalesce, ac, b)
end
function Base.broadcasted(::typeof(coalesce), a::ShiftedArray, b::AbstractArray)
error("coalescing a shifted array with another array is intentionally not supported. Please collect before coalescing.")
end
function Base.broadcasted(::typeof(coalesce), a, b::ShiftedArray)
error("coalescing with a ShiftedArray is intentionally not supported. Please collect before coalescing.")
end