# Change array type in mutating function?

**URL:** <https://discourse.julialang.org/t/change-array-type-in-mutating-function/52284>\
**Category:** General Usage\
**Created:** [December 23, 2020, 3:41pm UTC](https://discourse.julialang.org/t/change-array-type-in-mutating-function/52284 "2020-12-23T15:41:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [December 23, 2020, 3:41pm UTC](https://discourse.julialang.org/t/change-array-type-in-mutating-function/52284/1 "2020-12-23T15:41:41Z")

</div>

I am trying to define some simple functions for 2-D and 3-D rotations, and I would like to define a mutating version as well. This function

```julia
function rotate!(r, θ)
    r[:] = exp(θ*[0 1 ; -1 0])*r
end

```

works for arrays of floats, but fails for arrays of ints because the type of `r` is `Array{Int64}`, and I’m trying to fill it with floats.

Is there a way to have this function also mutate the array type? I tried a few things and couldn’t find a solution.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 23, 2020, 4:02pm UTC](https://discourse.julialang.org/t/change-array-type-in-mutating-function/52284/2 "2020-12-23T16:02:37Z")

</div>

No, the type of a value can never be mutated in Julia, so there’s no way to mutate an `Array{Int}` into an `Array{Float64}`. The easiest way around this is to just not pass an `Array{Int}` into your function.

If you want a convenient way to operate on `Int` array inputs, you could consider making both a mutating and non-mutating version of the function:

```julia
function rotate!(r)
  r .= 1.5
end

function rotate(r)
  out = similar(r, promote_type(eltype(r), Float64))
  rotate!(out)
  out
end

```

This is a common pattern in Julia, where the non-mutating function is just a convenient wrapper to allocate some output, call the mutating version on that output, and then return the result.

---

<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:** [December 23, 2020, 4:11pm UTC](https://discourse.julialang.org/t/change-array-type-in-mutating-function/52284/3 "2020-12-23T16:11:28Z")

</div>

No, the element type of an array is fixed.

In this specific case, the mutating function is not written properly.  
It allocates a number of temporary arrays, does the multiplication and writes the result back from a temporary array to the original. Whenever you have that pattern, it’s best to just return the temporary array instead of doing extra work of writing its contents into the original.

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [December 23, 2020, 4:17pm UTC](https://discourse.julialang.org/t/change-array-type-in-mutating-function/52284/4 "2020-12-23T16:17:49Z")

</div>

> [@rdeits](#):
>
> No, the type of a value can never be mutated in Julia, so there’s no way to mutate an `Array{Int}` into an `Array{Float64}` .

Okay, I had a suspicion that was the case. I did write up non-mutating versions, so I might try something like you suggest.

> [@Vasily\_Pisarev](#):
>
> It allocates a number of temporary arrays, does the multiplication and writes the result back from a temporary array to the original.

hmmm… okay, I’ll have to keep that in mind.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 23, 2020, 6:44pm UTC](https://discourse.julialang.org/t/change-array-type-in-mutating-function/52284/5 "2020-12-23T18:44:26Z")

</div>

> [@Vasily\_Pisarev](#):
>
> No, the element type of an array is fixed.

Well, you _can_ use `reinterpret`, and change the eltype while keeping the data. But that won’t do the right thing in this case.
