Empty field doesn't exist for arrays, right?

bearophile bearophileHUGS at lycos.com
Tue Mar 8 10:04:13 PST 2011


Andrej Mitrovic:

> 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:

You need this at the top:
import std.array;


> 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. 

emopty is not an array method, it's a free function that is used with a funny syntax.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list