# Why can't I modify the array in place when changing the type of array?

**URL:** <https://discourse.julialang.org/t/why-cant-i-modify-the-array-in-place-when-changing-the-type-of-array/18775>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 18, 2018, 1:42am UTC](https://discourse.julialang.org/t/why-cant-i-modify-the-array-in-place-when-changing-the-type-of-array/18775 "2018-12-18T01:42:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)\
**Post date:** [December 18, 2018, 1:42am UTC](https://discourse.julialang.org/t/why-cant-i-modify-the-array-in-place-when-changing-the-type-of-array/18775/1 "2018-12-18T01:42:26Z")

</div>

Hello, consider the following example:

```julia
julia> using SharedArrays

julia> function foo1(a::SharedArray)
       fill!(a, 4.0)
       end
foo1 (generic function with 1 method)

julia> function foo2(a::AbstractArray)
       a = SharedArray(a)
       foo1(a)
       end
foo2 (generic function with 1 method)

julia> a1 = SharedArray{Float64}(5, 5);

julia> a2 = Array{Float64}(undef, 5, 5);

julia> foo1(a1);

julia> a1
5×5 SharedArray{Float64,2}:
 4.0 4.0 4.0 4.0 4.0
 4.0 4.0 4.0 4.0 4.0
 4.0 4.0 4.0 4.0 4.0
 4.0 4.0 4.0 4.0 4.0
 4.0 4.0 4.0 4.0 4.0

julia> foo2(a2);

julia> a2
5×5 Array{Float64,2}:
 2.3586e-314 2.35848e-314 2.33899e-314 2.33899e-314 2.33899e-314
 2.35897e-314 2.359e-314 2.33899e-314 2.33899e-314 2.33899e-314
 2.35872e-314 2.359e-314 2.33899e-314 2.33899e-314 2.33899e-314
 2.35844e-314 2.35848e-314 2.33899e-314 2.33899e-314 2.33899e-314
 2.3588e-314 2.35848e-314 2.33899e-314 2.33899e-314 2.33899e-314

```

I thought `foo2` would give the modified array instead of the undef one since I overwrite the array with the new one in the function. Is it due to that I can only change the element value but cannot change the type of the array (from a usual array to `SharedArray`) in such case? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [December 18, 2018, 3:21am UTC](https://discourse.julialang.org/t/why-cant-i-modify-the-array-in-place-when-changing-the-type-of-array/18775/2 "2018-12-18T03:21:23Z")

</div>

Not at a computer, so I can’t test, but I _think_ your `a = SharedArray(a)` in foo2 is assigning a new local variable `a` inside the function scope rather than updating the global `a` assignment. If you put a `global` in front of it, I think it will work, but that’s not really changing it “in place,” just reassigning the variable to a new array.

I might also be full of crap - it’s late and I probably should be asleep 😐

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 18, 2018, 3:39am UTC](https://discourse.julialang.org/t/why-cant-i-modify-the-array-in-place-when-changing-the-type-of-array/18775/3 "2018-12-18T03:39:02Z")

</div>

`SharedArray(a)` copies `a` into the `SharedArray`.
