# How to predict the type of a view(A, ...)

**URL:** <https://discourse.julialang.org/t/how-to-predict-the-type-of-a-view-a/15200>\
**Category:** General Usage\
**Created:** [September 19, 2018, 9:49pm UTC](https://discourse.julialang.org/t/how-to-predict-the-type-of-a-view-a/15200 "2018-09-19T21:49:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [September 19, 2018, 9:49pm UTC](https://discourse.julialang.org/t/how-to-predict-the-type-of-a-view-a/15200/1 "2018-09-19T21:49:55Z")

</div>

I often want to pre-allocate a container for a bunch of views, but have no idea how to predict the type that `view` will return, and therefore don’t know how to make a container for them. I understand that this may be difficult to do in an extremely general way, but often my views end up being the same type anyways (e.g. a bunch of slices from the same array).

Any pointers?

---

<div class="post-metadata">

**Author:** ![Juser](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Juser](https://discourse.julialang.org/u/Juser)\
**Post date:** [September 19, 2018, 11:10pm UTC](https://discourse.julialang.org/t/how-to-predict-the-type-of-a-view-a/15200/2 "2018-09-19T23:10:53Z")

</div>

I typically just do `typeof` on an example. This is not a good generalizeable solution but is very cheap to implement even if you don’t understand anything about the `view` method or `SubArray` type (which I don’t).

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [September 20, 2018, 12:42am UTC](https://discourse.julialang.org/t/how-to-predict-the-type-of-a-view-a/15200/3 "2018-09-20T00:42:11Z")

</div>

Yeah that’s what I do too, but I wish there was some better way…

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 20, 2018, 1:30am UTC](https://discourse.julialang.org/t/how-to-predict-the-type-of-a-view-a/15200/4 "2018-09-20T01:30:39Z")

</div>

Effectively the same, but how about:

```julia
julia> allocate(x::T, n) where {T} = Vector{T}(undef, n)
allocate (generic function with 1 method)

julia> allocate(3, 10)
10-element Array{Int64,1}:
 4570098240
 4570098160
          1
 4570098320
 4570098400
 4570098480
          3
          7
          2
          1

julia> allocate("hello", 10)
10-element Array{String,1}:
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef

julia> M = [1 2; 3 4]
2×2 Array{Int64,2}:
 1 2
 3 4

julia> v = view(M, 1:2, 1:1)
2×1 view(::Array{Int64,2}, 1:2, 1:1) with eltype Int64:
 1
 3

julia> allocate(v, 10)
10-element Array{SubArray{Int64,2,Array{Int64,2},Tuple{UnitRange{Int64},UnitRange{Int64}},false},1}:
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef
 #undef

```

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [September 20, 2018, 2:10am UTC](https://discourse.julialang.org/t/how-to-predict-the-type-of-a-view-a/15200/5 "2018-09-20T02:10:29Z")

</div>

Clever solution! I may start using something similar to that. I think the “pain” (which is really not that bad) to either your `allocate` or doing what @Juser suggested is that you have to break up for loops. You have to run the first element, pre-allocate, put the first element into the array, then run the rest. It’s fine for one offs but I find myself turning to `map` quite often to avoid having to do all of that.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 20, 2018, 2:18am UTC](https://discourse.julialang.org/t/how-to-predict-the-type-of-a-view-a/15200/6 "2018-09-20T02:18:39Z")

</div>

Sounds like it could be a case for broadcasting (`.`).

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [September 20, 2018, 12:47pm UTC](https://discourse.julialang.org/t/how-to-predict-the-type-of-a-view-a/15200/7 "2018-09-20T12:47:42Z")

</div>

Is there a benefit to broadcasting when you don’t need to expand a dimension? I assumed that broadcasting and map would have similar performance.
