# Reinterpret zero-dimensional view

**URL:** <https://discourse.julialang.org/t/reinterpret-zero-dimensional-view/45047>\
**Category:** General Usage\
**Created:** [August 16, 2020, 2:36pm UTC](https://discourse.julialang.org/t/reinterpret-zero-dimensional-view/45047 "2020-08-16T14:36:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [August 16, 2020, 2:36pm UTC](https://discourse.julialang.org/t/reinterpret-zero-dimensional-view/45047/1 "2020-08-16T14:36:50Z")

</div>

Reinterpreting a zero-dimensional view:

```julia
a = [1; 2; 3]
b = view(a, 2)
c = reinterpret(UInt8, b) # ERROR: ArgumentError: cannot reinterpret a zero-dimensional `Int64` array to `UInt8` which is of a different size

```

The workaround is to add reshape:

```julia
a = [1; 2; 3]
b = view(a, 2)
c = reshape(b, 1)
d = reinterpret(UInt8, c)
#=
8-element reinterpret(UInt8, reshape(view(::Array{Int64,1}, 2), 1)):
 0x02
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
=#

```

1. Is there a better way than this to change how the compiler sees the type of a bunch of bits?
2. If not, is there a reason `reinterpret` needs help to handle this?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 16, 2020, 2:40pm UTC](https://discourse.julialang.org/t/reinterpret-zero-dimensional-view/45047/2 "2020-08-16T14:40:27Z")

</div>

Is this an alternative?

```julia
julia> b = view(a, 2:2)
1-element view(::Array{Int64,1}, 2:2) with eltype Int64:
 2

```

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [August 16, 2020, 4:17pm UTC](https://discourse.julialang.org/t/reinterpret-zero-dimensional-view/45047/3 "2020-08-16T16:17:31Z")

</div>

I guess that works, but my next question would be why there’s a difference in output between `view(a, 2:2)` and `view(a, 2)`. Is there a point to a zero-dimensional view?

---

<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:** [August 16, 2020, 4:29pm UTC](https://discourse.julialang.org/t/reinterpret-zero-dimensional-view/45047/4 "2020-08-16T16:29:39Z")

</div>

It’s the same as for ordinary slicing. `a[2]` is a scalar (as long as the eltype of `a` is), while `a[2:2]` is a vector. This is because `2` and `2:2` are of different types.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 16, 2020, 4:38pm UTC](https://discourse.julialang.org/t/reinterpret-zero-dimensional-view/45047/5 "2020-08-16T16:38:40Z")

</div>

Yes, a 0-dimensional view is definitely sometimes useful, because it behaves just like the `Ref` type, but still refers to the original array. You can pass `view(a, 2)` to a function and that function can then modify the second entry of `a` with `v[] = ...`, where `v` is the name of the view. This can be useful for emulating the pass-by-reference behavior in C++, for example.

---

<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:** [August 17, 2020, 4:11pm UTC](https://discourse.julialang.org/t/reinterpret-zero-dimensional-view/45047/6 "2020-08-17T16:11:24Z")

</div>

It’s an interesting case because `reinterpret` extends the first dimension to fit all the new elements… but there is no first dimension in a 0d array. I’ve often thought it’d be helpful for it to _add_ a dimension instead, which would completely avoid this problem, but is of course breaking.
