# Indexing with Composite Type

**URL:** <https://discourse.julialang.org/t/indexing-with-composite-type/53511>\
**Category:** New to Julia\
**Created:** [January 18, 2021, 1:51am UTC](https://discourse.julialang.org/t/indexing-with-composite-type/53511 "2021-01-18T01:51:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephenll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephenll/32/28751_2.png) [@stephenll](https://discourse.julialang.org/u/stephenll)\
**Post date:** [January 18, 2021, 1:51am UTC](https://discourse.julialang.org/t/indexing-with-composite-type/53511/1 "2021-01-18T01:51:07Z")

</div>

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

For ex:

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 18, 2021, 1:54am UTC](https://discourse.julialang.org/t/indexing-with-composite-type/53511/2 "2021-01-18T01:54:46Z")

</div>

All you need to do here is define

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

```

---

<div class="post-metadata">

**Author:** ![stephenll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephenll/32/28751_2.png) [@stephenll](https://discourse.julialang.org/u/stephenll)\
**Post date:** [January 18, 2021, 2:27am UTC](https://discourse.julialang.org/t/indexing-with-composite-type/53511/3 "2021-01-18T02:27:09Z")

</div>

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:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [January 18, 2021, 3:43am UTC](https://discourse.julialang.org/t/indexing-with-composite-type/53511/4 "2021-01-18T03:43:43Z")

</div>

Broadcasting `setfield!` uses the `Base.dotview` method:

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

```
