View mode: basic / threaded / horizontal-split · Log in · Help
March 30, 2011
Status of $ in class/struct?
I haven't been following the $/__dollar/opDollar/etc chronicles very 
closely, and I can't find the answer in the docs, but I have simple 
question:

Suppose I have this:

-------------------------------
class Foo
{
   int[] data;

   this(int[] data)
   {
       this.data = data;
   }

   ref T opIndex(size_t i)
   {
       return data[i];
   }
}

void main()
{
   auto foo = Foo([1,2,3,4,5]);
   ...
}
-------------------------------

Question:

Is there currently any way for Foo's author to make "foo[$-1]" work? If so, 
how?
March 30, 2011
Re: Status of $ in class/struct?
I think its broken or something. It looks like its trying to find
__dollar in the current scope. Because this non-solution seems to
work:

module opDollarTest;

import std.stdio;

class Foo
{
   int[] data;

   this(int[] data)
   {
       this.data = data;
   }

   int opIndex(int a)
   {
       return data[a];
   }

   @property
   size_t length()
   {
       return data.length;
   }

   @property
   size_t opDollar()
   {
       return length;
   }
}

void main()
{
   auto foo = new Foo([1,2,3,4,5]);

   size_t __dollar()
   {
       return foo.opDollar();
   }

   writeln(foo[$-1]);
}
March 30, 2011
Re: Status of $ in class/struct?
On 2011-03-29 19:29, Nick Sabalausky wrote:
> I haven't been following the $/__dollar/opDollar/etc chronicles very
> closely, and I can't find the answer in the docs, but I have simple
> question:
[snip]
> Question:
> 
> Is there currently any way for Foo's author to make "foo[$-1]" work? If so,
> how?

It's not implemented yet: http://d.puremagic.com/issues/show_bug.cgi?id=3474

- Jonathan M Davis
Top | Discussion index | About this forum | D home