Phobos Ranges

You will be acquainted with the Phobos range in this lesson.

Introduction

The ranges in this chapter are different from number ranges that are written in the form begin…end. We have discussed how number ranges are used with the foreach loop and with slices:

foreach (value; 3..7) {     // number range,
// NOT a Phobos range
int[] slice = array[5..10]; // number range,
                            // NOT a Phobos range

When we write a range in this chapter, we mean a Phobos range.

Ranges form a range hierarchy. At the bottom of this hierarchy is the simplest range: InputRange. The other ranges bring more requirements on top of the range on which they are based. The following are all of the ranges with their requirements, sorted from the simplest to the more capable:

  • InputRange: requires the empty(), front(), and popFront() member functions

  • ForwardRange: additionally requires the save() member function

  • BidirectionalRange: additionally requires the back() and popBack() member functions

  • RandomAccessRange: additionally requires the [] operator (and another property depending on whether the range is finite or infinite)

This hierarchy can be shown as in the following graph. RandomAccessRange has finite and infinite versions:

Get hands-on with 1200+ tech skills courses.