Wrapper type of static array, how to expose iterator

Here’s my working solution for anyone stumbling here. Based on this other discourse post. The main issue I had is the documentation doesn’t describe how to implement broadcasting for immutable types, such as when the backing vector is a SVector.

struct Point{N, T<:Number} <: AbstractArray{T, 1}
    p::SVector{N, T}
end; export Point

# Broadcasting
import Base.broadcastable
Base.broadcastable(point::Point) = point
struct PointStyle{N} <: Broadcast.BroadcastStyle end
import Base.Broadcast.BroadcastStyle 
Base.Broadcast.BroadcastStyle(::Type{Point{N,T}}) where {N,T} = PointStyle{N}()
Base.Broadcast.BroadcastStyle(::PointStyle{N}, ::Broadcast.DefaultArrayStyle{0}) where N = PointStyle{N}()
import Base.Broadcast.materialize
function Base.Broadcast.materialize(B::Broadcast.Broadcasted{PointStyle{N}}) where N
    flat = Broadcast.flatten(B)
    args = flat.args
    f = flat.f
    ps = map(x -> x isa Point ? x.p : Ref(x), args)
    Point(f.(ps...))
end
1 Like