What's the deal with __buck?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jan 28 20:37:44 PST 2009


I stumbled upon a very interesting problem. Consider an infinite range 
that generates the numbers 1, 2, 3, ...

That range doesn't have a "length" member. However, it is a random 
access range, which makes things rather interesting. Now consider I want 
to advance 10 steps in that range. Being an obedient D programmer I'd write:

auto r = iota(1);
// skip 10 steps
r = r[10 .. $];

Now this is very cool. If a range is infinite, I can use the $ symbol in 
the right position, but nothing else. So I tried to effect that and got 
the following to compile:

struct DollarType {}
enum DollarType __dollar = DollarType();

struct S
{
     void opSlice(uint, DollarType)
     {
     }
}

void main()
{
     S s;
     s[0 .. $];
}

This is cool because it allows detection of passing $. Now the problem 
is, I can't seem to get rid of the __dollar definition! Has anyone found 
a general-enough trick? I'd want a[...$...] to morph the $ into a.length 
*iff* a defines length, and go with the __dollar otherwise.


Thanks,

Andrei



More information about the Digitalmars-d mailing list