Search⌘ K
AI Features

ForwardRange and BidirectionalRange

Understand how ForwardRange and BidirectionalRange improve data handling and iteration control in D programming. Explore how ForwardRange allows creating independent copies of ranges and how BidirectionalRange enables accessing elements from both ends, helping you write efficient and flexible programs.

We'll cover the following...

ForwardRange

InputRange models a range where elements are taken out of the range as they are iterated over.

Some ranges are capable of saving their states, as well as operating as an InputRange. For example, FibonacciSeries objects can save their states because these objects can freely be copied, and the two copies continue their lives independently from each other.

ForwardRange provides the save member function, which is expected to return a copy of the range. The copy that save returns must operate independently from the range object that it was copied from; iterating over one copy must not affect the other copy.

Importing std.array automatically makes slices become ForwardRange ranges.

In order to implement save for FibonacciSeries, we can simply return a copy of the object:

struct
...