# Using reshape with UInt16 for dims

**URL:** https://discourse.julialang.org/t/using-reshape-with-uint16-for-dims/17616
**Category:** Internals & Design
**Created:** [November 16, 2018, 8:25pm UTC](https://discourse.julialang.org/t/using-reshape-with-uint16-for-dims/17616 "2018-11-16T20:25:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![standarddeviant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/standarddeviant/32/2309_2.png) [@standarddeviant](https://discourse.julialang.org/u/standarddeviant)
#### Post date: [November 16, 2018, 8:25pm UTC](https://discourse.julialang.org/t/using-reshape-with-uint16-for-dims/17616/1 "2018-11-16T20:25:05Z")

</div>

I was surprised to find out that using `UInt16`s is not a valid way to reshape an `Array`. I get the following behavior on Julia 0.6.4 as well as Julia 1.0.2

```julia
julia> a = collect(1:6)
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

julia> b = reshape(a, 3, 2)
3×2 Array{Int64,2}:
 1 4
 2 5
 3 6

julia> c = reshape(a, UInt16(3), UInt16(2))
ERROR: MethodError: no method matching reshape(::Array{Int64,1}, ::UInt16, ::UInt16)
Closest candidates are:
  reshape(::Array{T,N}, ::Tuple{Vararg{Int64,N}}) where {N, T} at array.jl:303
  reshape(::Array{T,N} where N, ::Tuple{Vararg{Int64,N}}) where {N, T} at array.jl:314
  reshape(::AbstractArray, ::Int64...) at reshapedarray.jl:99
  ...
Stacktrace:
 [1] top-level scope at none:0

julia>

```

Is this intended? I’m reading a `UInt16` from a network stream as information to reshape incoming data and I now have to cast to explicitly convert my `UInt16`’s to `Int32`’s.

As an aside, I do enjoy the irony that reshaping with negative numbers seems invalid, but `Int32` allows for that 🙂

---

<div class="post-metadata">

### Author: ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)
#### Post date: [November 16, 2018, 8:37pm UTC](https://discourse.julialang.org/t/using-reshape-with-uint16-for-dims/17616/2 "2018-11-16T20:37:13Z")

</div>

It works, but you need to pass the dims as a tuple.  
`c = reshape(1:6, (UInt16(3), UInt16(2)))`.

---

<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: [November 16, 2018, 8:41pm UTC](https://discourse.julialang.org/t/using-reshape-with-uint16-for-dims/17616/3 "2018-11-16T20:41:23Z")

</div>

Yeah, ensuring we support all the different permutations of integer types as dimension lengths through all of our APIs is a challenge. Looks like we missed this case.

---

<div class="post-metadata">

### Author: ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)
#### Post date: [December 9, 2018, 7:52am UTC](https://discourse.julialang.org/t/using-reshape-with-uint16-for-dims/17616/4 "2018-12-09T07:52:45Z")

</div>

Am I missing something in how this works? It looks like `Int64` to me even after the reshape.

```julia
julia> a
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

julia> b = reshape(a, (UInt16(3), UInt16(2)))
3×2 Array{Int64,2}:
 1 4
 2 5
 3 6

julia> b = reshape(1:6, (UInt16(3), UInt16(2)))
3×2 reshape(::UnitRange{Int64}, 3, 2) with eltype Int64:
 1 4
 2 5
 3 6

```

---

<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 9, 2018, 12:33pm UTC](https://discourse.julialang.org/t/using-reshape-with-uint16-for-dims/17616/5 "2018-12-09T12:33:08Z")

</div>

Reshape doesn’t change the element type. Perhaps you want `UInt16.(b)` ?

---

<div class="post-metadata">

### Author: ![standarddeviant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/standarddeviant/32/2309_2.png) [@standarddeviant](https://discourse.julialang.org/u/standarddeviant)
#### Post date: [December 10, 2018, 4:02pm UTC](https://discourse.julialang.org/t/using-reshape-with-uint16-for-dims/17616/6 "2018-12-10T16:02:16Z")

</div>

@oliver My original question is concerning the `eltype` of the supplied, new dimensions, not the `eltype` of the reshaped array.
