array length vs $

Janice Caron caron800 at googlemail.com
Sat Mar 15 08:17:13 PDT 2008


On 15/03/2008, Frank Benoit <keinfarbton at googlemail.com> wrote:
>  Removing 'length' would not break code in unexpected ways. It would
>  yield compile errors in every case and can be fixed by simple replacing
>  'length' to '$'.

I'm inclined to agree. "length" is a property all arrays. You can say,
for example,

    int n = array.length;

but not

    int n = array.$;

However, within array square brackets, "length" implicitly means
"array.length", and this is unlike any other member variable in D. For
example, the following will not compile:

    auto array = new int[4];
    int n = array[ptr-ptr];

Both "length" and "ptr" are member variables of array, but within
those square brackets, "length" is implicitly known, and "ptr" is not.
That's inconsistent. And as Frank said, it's been superceded by "$".

BUT...

I want to be able to create containers that behave just like the built
in ones. That means, I want the $ symbol to be allowed within the
square brackets of user-defined structs and classes, and that it
should call the member function length() when referenced. I see that
as a critical piece of the jigsaw.



More information about the Digitalmars-d mailing list