# Type of array views

**URL:** <https://discourse.julialang.org/t/type-of-array-views/44476>\
**Category:** New to Julia\
**Created:** [August 7, 2020, 7:26am UTC](https://discourse.julialang.org/t/type-of-array-views/44476 "2020-08-07T07:26:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [August 7, 2020, 7:26am UTC](https://discourse.julialang.org/t/type-of-array-views/44476/1 "2020-08-07T07:26:34Z")

</div>

Hi,

I have just discovered the catch with passing array slices to a function that is to modify the array entries. “view” is the solution. ([9792](https://discourse.julialang.org/t/why-array-slices-cant-be-passed-to-a-function-by-reference/9792)).

The rub is, that while c[:,i] is a plain ol’ vector, view(c,:,i) has some complicated type. Logical enough, but throws a spanner in the works of my dispatch system.

```julia
function init!(a)
    for i = 1:2
        a[i] = 1. *i
    end
end
function initstrict!(a::Vector{Float64})
    for i = 1:2
        a[i] = 1. *i
    end
end

a = Array{Float64,1}(undef,2)
init!(a) # a is initialized
b = Array{Float64,1}(undef,2)
init!(b[:]) # deepcopy of b passed, b not initialised
c = Array{Float64,2}(undef,2,3)
init!(view(c,:,1)) # view to the rescue, 1st column initialised
c = Array{Float64,2}(undef,2,3) 
initstrict!(view(c,:,1)) # uh oh, type mismatch!

```

I can workaround the issue with

```julia
c = Array{Float64,2}(undef,2,3) 
a = Array{Float64,1}(undef,2)
initstrict!(a)
c[:,1] = a

```

but that’s a deepcopy and more code. I do not consider it an option to rewrite initstrict! to accept (“vectors or any vector-like view”).

There are other workarounds in my specific case, but I am here to learn: passing an array slice, easy method dispatching and no deepcopy? Yeah, can I have my slice _and_ eat it?

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [August 7, 2020, 8:01am UTC](https://discourse.julialang.org/t/type-of-array-views/44476/2 "2020-08-07T08:01:31Z")

</div>

> [@Philippe\_Maincon1](#):
>
> I do not consider it an option to rewrite initstrict! to accept (“vectors or any vector-like view”)

Why not? Is it important that the function accepts only `Vector{Float64}` and not `AbstractVector{Float64}`?

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [August 7, 2020, 8:03am UTC](https://discourse.julialang.org/t/type-of-array-views/44476/3 "2020-08-07T08:03:10Z")

</div>

Great, if its as simple as “AbstractVector”, that’s great. I was afraid of some ugly Union of whatnot where…

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [August 7, 2020, 8:55am UTC](https://discourse.julialang.org/t/type-of-array-views/44476/4 "2020-08-07T08:55:42Z")

</div>

Is the literal range `1:2` important?

If you just want to initialize the values with the corresponding indices, then `a .= eachindex(a)` or `a .= 1:length(a)` would be more appropriate.

And there is no need using C idioms like `1. * i` when assigning `Int`s into a `Float` array, the rhp is automatically converted to the array element type (using `convert`). For an explicit conversion, `a[i] = convert(Float64, i)` or `a[i] = Float64(i)` would be preferred.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [August 7, 2020, 9:00am UTC](https://discourse.julialang.org/t/type-of-array-views/44476/5 "2020-08-07T09:00:40Z")

</div>

Hi Vasily,  
No, no the literal range was just laziness in writing up my question - in real code I would indeed have used eachindex (lazy in the sense that the reflexes are not quite rubbed in yet!).

Point taken on conversions.

Thank you!
