Pointers and Ranges
    Ali Çehreli 
    acehreli at yahoo.com
       
    Sat Oct  8 10:45:20 PDT 2011
    
    
  
On Sat, 08 Oct 2011 12:31:20 -0400, Xinok wrote:
> I'm new to ranges and while I understand the basic concept, I don't know
> everything about them. With arrays, you can simply use arr.ptr to infer
> a type automatically.
Or with ElementType. If Range is a template type:
   writefln("I have a range of %ss", (ElementType!Range).stringof);
// for example, "I have a range of ints"
> So I was wondering, is there an equivalent for
> ranges? What I'm looking for is the ability to do *p
Depending on the range it can be
- myRange.front
- myRange.back
- myRange[i]
> as well as p[1] or
Only with RandomAccessRanges.
> p[-1] with ranges.
That should be considered out of bounds with ranges, but it is possible 
to achieve with opIndex() for RandomAccessRanges.
Ali
P.S. I am in the process of translating my Turkish D book to English. 
Since it targets the beginner programmer and starts from the very basics, 
it may be a little boring for some. But I had decided to skip some 
chapters and start translating more interesting ones that normally come 
later in the book. The two chapters on Ranges happen to be the ones that 
I am currently working on. I may make the current state of the 
translation available in a few days. Here are the originals:
  http://ddili.org/ders/d/araliklar.html
  http://ddili.org/ders/d/araliklar_baska.html
There is a Google translate bar on the left hand side with very limited 
results. (Humans are still better than computers! :))
Here is a little ForwardRange example:
import std.stdio;
import std.range;
struct FibonacciSeries
{
    int first = 0;
    int second = 1;
    static enum empty = false;
    // Alternatively: static immutable bool empty = false;
    @property int front() const
    {
        return first;
    }
    void popFront()
    {
        int third = first + second;
        first = second;
        second = third;
    }
    FibonacciSeries save() const
    {
        return this;
    }
}
void main()
{
    writeln(take(FibonacciSeries(), 10));
}
    
    
More information about the Digitalmars-d-learn
mailing list