Indexing with Composite Type

Is there a way to easily create a Composite type where all indexing would apply to a ‘Matrix’ field in the type?

For ex:

Mutable Struct Foo
    hist::Int
    matrix::AbstractMatrix
end

bar = Foo(10,rand(10,10))

I would like to be able to bar[1:5,2:6] or any of there other ways to index into, assign bar[bar.>.5] .= 123 , create views, where the indexing/setting would be done on bar.matrix. Of course there would be other methods that rely on the hist field and the matrix field.

–
Stephen

All you need to do here is define

import Base: getindex, setindex!
getindex(x::Foo, inds...) = getindex(x.matrix, inds...)
setindex!(x::Foo, v, inds...) = setindex!(x.matrix, v, inds...)

Thank you. This gets me close.

I added a view(x::Foo, inds...) = view(x.matrix, inds...)

but for some reason bar[bar.matrix .> .5] .= 555 does not work. Here is the all the code:

module sll
import Base: getindex, setindex!, view

mutable struct Foo
    hist::Int
    matrix::AbstractMatrix
end

getindex(x::Foo, inds...) = getindex(x.matrix, inds...)
setindex!(x::Foo, v, inds...) = setindex!(x.matrix, v, inds...)
view(x::Foo, inds...)  = view(x.matrix, inds...)

end

bar = sll.Foo(10, rand(10,10))
bar[bar.matrix .> .5] .= 555 

# that doesn't seem to work.  nothing in bar.matrix changed to 555 
# even though some of the values where greater than .5

Broadcasting setfield! uses the Base.dotview method:

Base.dotview(foo::Foo, inds...) = Base.dotview(foo.matrix, inds...)