# Show for vector type that defines matrix getindex

**URL:** <https://discourse.julialang.org/t/show-for-vector-type-that-defines-matrix-getindex/23732>\
**Category:** General Usage\
**Created:** [May 1, 2019, 12:10pm UTC](https://discourse.julialang.org/t/show-for-vector-type-that-defines-matrix-getindex/23732 "2019-05-01T12:10:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 1, 2019, 12:10pm UTC](https://discourse.julialang.org/t/show-for-vector-type-that-defines-matrix-getindex/23732/1 "2019-05-01T12:10:29Z")

</div>

I have a type that wraps a vector. I have defined two-argument `getindex` for vectors of this type, i.e.,  
`getindex(v::Vector{MyVec}, i::Int, j::Int) = v[j][i]`. This works well, but it messes up REPL display of one of these vectors, MWE below. The error seems to be that `print_matrix` is called, in which the two-argument `getindex` is called, even though it’s a vector being printed. What is the cleanest way to ensure that the printing of `Vector{MyVec}` is correct?

```julia
julia> struct MyVec
         v::Vector
       end

julia> Base.getindex(v::MyVec, i::Int) = v.v[i]

julia> Base.getindex(v::Vector{MyVec}, i::Int, j::Int) = v[j][i]

julia> v = MyVec([1,2])
MyVec([1, 2])

julia> vv = [v,v] # This does not produce the correct output!
2-element Array{MyVec,1}:
 1
 2

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 1, 2019, 12:22pm UTC](https://discourse.julialang.org/t/show-for-vector-type-that-defines-matrix-getindex/23732/2 "2019-05-01T12:22:03Z")

</div>

The basic problem here is that your `getindex(v::Vector{MyVec}, i::Int, j::Int)` breaks how vectors work. In order to let the same code work with either vectors or single-column matrices, arrays are treated as if they had an infinite number of trailing singleton dimensions:

```julia
julia> x = rand(3)
3-element Array{Float64,1}:
 0.5208526911072409 
 0.42943207563780583
 0.6718262282859668 

julia> x[2,1]
0.42943207563780583

julia> x[2,1,1]
0.42943207563780583

julia> [size(x,i) for i = 1:3]
3-element Array{Int64,1}:
 3
 1
 1

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 1, 2019, 12:28pm UTC](https://discourse.julialang.org/t/show-for-vector-type-that-defines-matrix-getindex/23732/3 "2019-05-01T12:28:46Z")

</div>

Alright, so defining this `getindex` method is a bad idea then, thanks for clarifying!
