# Unusual empty \`UnitRange\`s while reinterpreting a vector

**URL:** <https://discourse.julialang.org/t/unusual-empty-unitrange-s-while-reinterpreting-a-vector/92546>\
**Category:** General Usage\
**Tags:** array, range\
**Created:** [January 5, 2023, 8:54am UTC](https://discourse.julialang.org/t/unusual-empty-unitrange-s-while-reinterpreting-a-vector/92546 "2023-01-05T08:54:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [January 5, 2023, 8:54am UTC](https://discourse.julialang.org/t/unusual-empty-unitrange-s-while-reinterpreting-a-vector/92546/1 "2023-01-05T08:54:43Z")

</div>

```julia
julia> r = reinterpret(UnitRange{Int}, [1,-1])[1]
1:-1

julia> isempty(r)
true

julia> length(r) # source of errors
-1

julia> 1:-1 # the constructor changes the endpoint to ensure that the length is zero
1:0

julia> r == 1:0
true

julia> collect(r) # doesn't work, as the length is negative
ERROR: ArgumentError: invalid Array dimensions

```

I know why this is happening, but I wonder if there’s a way around this?

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 5, 2023, 9:56am UTC](https://discourse.julialang.org/t/unusual-empty-unitrange-s-while-reinterpreting-a-vector/92546/2 "2023-01-05T09:56:18Z")

</div>

Can you use this fact?

```julia
julia> r == 1:0
true

julia> r === 1:0
false

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [January 5, 2023, 10:23am UTC](https://discourse.julialang.org/t/unusual-empty-unitrange-s-while-reinterpreting-a-vector/92546/3 "2023-01-05T10:23:19Z")

</div>

Not really in general. The issue downstream in this example is that

```julia
isempty(a::AbstractArray) = (length(a) == 0)

```

which requires the length to be zero. Perhaps this may be changed to `length(a) <= 0`?

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [January 5, 2023, 11:17am UTC](https://discourse.julialang.org/t/unusual-empty-unitrange-s-while-reinterpreting-a-vector/92546/4 "2023-01-05T11:17:52Z")

</div>

It seems to me that by using `reinterpret` in this way, you are making a corrupted instance of `UnitRange` (corrupted in the sense that it violates an invariant that is guaranteed by the inner constructor).

You found one issue with the broken invariant. Even if you work around this, who knows which other parts might break, maybe silently…

Do you have a very very good reason to do this? Otherwise I think it’s a bad idea to try and patch this in your code, and a worse idea to patch it in the standard library.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [January 5, 2023, 5:25pm UTC](https://discourse.julialang.org/t/unusual-empty-unitrange-s-while-reinterpreting-a-vector/92546/5 "2023-01-05T17:25:51Z")

</div>

`reinterpret` should be used with care. It literally places the bits of one object into the bits of another and nothing more. Note that `1:-1` is not a well-formed `UnitRange` and would normally be impossible to create (if you attempt to create `1:-1` it returns `1:0`) and only exists because you bypassed the constructor via `reinterpret`. This is why you’re finding broken functionality.

I’d suggest you splat your two-element vector into `UnitRange` like this, if you must make this sort of conversion:

```julia-repl
julia> UnitRange([1,-1]...)
1:0

```
