# Typeof view

**URL:** <https://discourse.julialang.org/t/typeof-view/16349>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 15, 2018, 4:06pm UTC](https://discourse.julialang.org/t/typeof-view/16349 "2018-10-15T16:06:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 15, 2018, 4:06pm UTC](https://discourse.julialang.org/t/typeof-view/16349/1 "2018-10-15T16:06:25Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [October 15, 2018, 4:13pm UTC](https://discourse.julialang.org/t/typeof-view/16349/2 "2018-10-15T16:13:41Z")

</div>

> [@e3c6](#):
>
> 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}

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:

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

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 15, 2018, 4:15pm UTC](https://discourse.julialang.org/t/typeof-view/16349/3 "2018-10-15T16:15:13Z")

</div>

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