# Show for vector type that defines matrix getindex

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

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> 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
``````
1 Like

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