What's the deal with __buck?

Bill Baxter wbaxter at gmail.com
Wed Jan 28 20:48:45 PST 2009


On Thu, Jan 29, 2009 at 1:37 PM, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> wrote:
> 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.

You need to give your DollarType an int and some arithmetic operators
and have it represent an /offset/ from the end rather than signifying
the end itself.
Otherwise  a[$-1] can't work.

Once you do that, then you can do things like
struct S
{
   T opSlice(uint a,  DollarType b) {
      return opSlice(a, length+b.offset);
   }
   T opSlice(uint a, uint b) {
           ....
   }
   ...
}

--bb



More information about the Digitalmars-d mailing list