Typeof view

I am creating a struct that contains an array, and views into this array. I need to specify the type of the view when declaring the struct, but:

julia> X = rand(10,5,6);

julia> typeof(@view X[:,:,1:4])
SubArray{Float64,3,Array{Float64,3},Tuple{Base.Slice{Base.OneTo{Int64}},Base.Slice{Base.OneTo{Int64}},UnitRange{Int64}},true}

The type of the view is a very complicated thing. I defined my own:

const ViewType = SubArray{T, N, Array{T,N}, 
                          Tuple{Base.Slice{Base.OneTo{Int64}},
                          Base.Slice{Base.OneTo{Int64}},
                          UnitRange{Int64}}, true
                          } where {T,N}

to work around this. But maybe there is already a definition like this in Julia that I am overlooking? Or a better way of doing what I want?

If I’m not mistaken, your ViewType is a UnionAll type, therefore your struct will not be concretely typed.

You can be lazy and just do something like:

struct mystruct{T,N,V1,V2} where {T,N,V1,V2}
    a::Array{T,N}
    view1::V1
    view2::V2
end
1 Like

That’s a nice approach. I can later infer the types V1,V2 from the arguments of the constructor.

Note that in the definition of the struct I was using ViewType{T,N}, so it’s no longer a UnionAll.

1 Like