# Return type of reshape

**URL:** <https://discourse.julialang.org/t/return-type-of-reshape/21060>\
**Category:** General Usage\
**Created:** [February 21, 2019, 9:17pm UTC](https://discourse.julialang.org/t/return-type-of-reshape/21060 "2019-02-21T21:17:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [February 21, 2019, 9:17pm UTC](https://discourse.julialang.org/t/return-type-of-reshape/21060/1 "2019-02-21T21:17:53Z")

</div>

Hello,

What are the rules that dictate the behavior of the `reshape` function. It seems that, most of the time, an `Array` object is returned. However, sometimes an `Base.ReshapedArray` object is returned. I have hard time figuring out when that happen? Also, what is the cost of converting an `Base.ReshapedArray` object to an `Array` object?

Thanks,

Maxime

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [February 21, 2019, 9:31pm UTC](https://discourse.julialang.org/t/return-type-of-reshape/21060/2 "2019-02-21T21:31:52Z")

</div>

Normally, you wouldn’t want to convert a `ReshapedArray` to a normal `Array`, any function that’s defined on an `AbstractArray` will work. Do you have some specific cases where this causes an issue?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 21, 2019, 9:42pm UTC](https://discourse.julialang.org/t/return-type-of-reshape/21060/3 "2019-02-21T21:42:50Z")

</div>

Custom array types can specialize `reshape` if they want — the key isn’t the return type but that the data that it accesses is _shared_ with the original array.

The cost of converting it back to an `Array` is that you need to do a complete copy of all the data into a brand new object.

In general, the solution to problems like this is to widen your function signatures — don’t write `f(::Array{Float64})`, instead write `f(::AbstractArray)`. You very often don’t need to specify `::Array` or its element type at all.

---

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [February 21, 2019, 9:56pm UTC](https://discourse.julialang.org/t/return-type-of-reshape/21060/4 "2019-02-21T21:56:27Z")

</div>

In fact, the problem is with the return of the function.

Imagine that I have this code

```julia
function f(mat::AbstractMatrix, to_reshape::Bool, ...) 
    if to_reshape
        return reshape(mat, ...)
    else 
        return mat
    end
end

```

Then, if `reshape` return a `ReshapedArray`, this function can either return an `Array` or a `ReshapedArray`. Right now, I have a weird bug where a variable take different values if I print it or not. So, I’m trying to remove all ambiguous compilation situations that may happen in my code.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [February 21, 2019, 10:01pm UTC](https://discourse.julialang.org/t/return-type-of-reshape/21060/5 "2019-02-21T22:01:56Z")

</div>

> [@mbauman](#):
>
> You very often don’t need to specify `::Array`

The most common case when you need it is if you want to guarantee that the argument is mutable (e.g. you can run `setindex!` on it or resize it). In such cases - from my experience using `reshape` is not the best option, which means that sometimes it is hard to recommend a good method that would universally work.

---

<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:** [February 21, 2019, 10:39pm UTC](https://discourse.julialang.org/t/return-type-of-reshape/21060/6 "2019-02-21T22:39:20Z")

</div>

> [@maxtremblay](#):
>
> Right now, I have a weird bug where a variable take different values if I print it or not. So, I’m trying to remove all ambiguous compilation situations that may happen in my code.

This is a very common symptom of some kind of memory access error. The most common ones in julia (in my experience) are:

- Using `@inbounds` with an out of bounds index
- Constructing an `Array(undef, ....)` and not actually filling it
- Doing something bad with a `ccall`

I would suggest focusing on those issues rather than worrying about whether you have an array or a reshaped array. For example, does the problem change if you run julia with `--check-bounds=yes` ?

---

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [February 22, 2019, 2:36pm UTC](https://discourse.julialang.org/t/return-type-of-reshape/21060/7 "2019-02-22T14:36:40Z")

</div>

> [@rdeits](#):
>
> –check-bounds=yes

This seems to work when I add this line. I will investigate this direction more, but I don’t have any of the three elements you mentioned in my code.

Also, it works when I do the following

```julia
julia --check-bounds=no Tests/run_tests.jl

```

But, it fails if I do

```julia
julia Tests/run_tests.jl

```

Thanks.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [February 22, 2019, 2:47pm UTC](https://discourse.julialang.org/t/return-type-of-reshape/21060/8 "2019-02-22T14:47:01Z")

</div>

Any `@simd` statements? I ran into an issue a little while ago where erroneously adding `@simd` in a place where it was not safe to execute iterations in arbitrary order caused a crash.

---

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [February 22, 2019, 3:15pm UTC](https://discourse.julialang.org/t/return-type-of-reshape/21060/9 "2019-02-22T15:15:24Z")

</div>

No `@simd`. Also, if I run the code using the REPL and `include(“Tests/run\_tests.jl”) the problem is gone.
