# Unify and expand Range and its likes

**URL:** <https://forums.swift.org/t/unify-and-expand-range-and-its-likes/38427>\
**Category:** Discussion\
**Created:** [July 14, 2020, 12:30am UTC](https://forums.swift.org/t/unify-and-expand-range-and-its-likes/38427 "2020-07-14T00:30:36Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [July 14, 2020, 9:01pm UTC](https://forums.swift.org/t/unify-and-expand-range-and-its-likes/38427/6 "2020-07-14T21:01:33Z")

</div>

> [@wowbagger](#):
>
> ```swift
> for i in (1...).reversed() {
> print(i)
> }
> 
> ```
> 
> It compiles, and runs non-stop. Instead of throwing an error, it just keeps running and never enters the for-loop body. Is this behaviour fixable with the current design?

`PartialRangeFrom` is not a `BidirectionalCollection`, so calling `reversed()` on it does not produce a `ReversedCollection`.

Instead it produces an `Array`.

The program hangs while attempting to materialize that array. This is exactly the same behavior you would see when converting any other very long (or endless) sequence into an array.

• • •

To “fix” the behavior, we could introduce a deprecated overload:

```swift
extension PartialRangeFrom where Bound: Strideable, Bound.Stride: SignedInteger {
  @available(*, deprecated, message: "Cannot reverse a PartialRangeFrom")
  func reversed() -> [Bound] { fatalError() }
}

```

That way attempting to call `reversed` on a `PartialRangeFrom` would give a compile-time warning and trap at runtime, instead of hanging.

The hang would still happen if `reversed` was called on such a value in a generic context, but such is life.

• • •

Interestingly, this code traps at runtime for me (Xcode 11.3.1, Swift 5.1.3):

```swift
let a = (1 as Int8)...
let b = a.reversed() // Execution was interrupted, reason: EXC_BAD_INSTRUCTION 
print(b.count)

```

And indeed so does this:

```swift
let a = Array((1 as Int8)...)

```

Or even this:

```swift
let a = ((1 as Int8)...).max()

```

The implication is clearly that partial ranges in fact model truly unbounded, one-sided intervals, not restricted to the set of representable values for the element type.

---

_[View the full topic](https://forums.swift.org/t/unify-and-expand-range-and-its-likes/38427)._
