www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Status of $ in class/struct?

reply "Nick Sabalausky" <a a.a> writes:
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?
Mar 29 2011
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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]);
}
Mar 29 2011
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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:

 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
Mar 29 2011