Empty field doesn't exist for arrays, right?

Andrej Mitrovic none at none.none
Tue Mar 8 09:56:05 PST 2011


module test;

struct MyArray(T)
{
    private T[] data;
    
    bool opCast(T)() if (is(T == bool))
    {
        return !data.empty;
    }
}

void main()
{
    auto foo = MyArray!(int)();
    auto state = foo ? true : false;
}

test.d(13): Error: undefined identifier module test.empty
test.d(20): Error: template instance test.MyArray!(int).MyArray.opCast!(bool) error instantiating

This is straight from the book. Did .empty exist for arrays before? Perhaps this was just a typo in the book, and it was supposed to be:

    bool opCast(T)() if (is(T == bool))
    {
        return data.length != 0;
    }

Also, that error message *really* needs to improve. It's not module 'test' which is missing the method, it's 'data'. This is one of the most confusing error messages that I know of and it pops up all the time. 


More information about the Digitalmars-d-learn mailing list