Empty field doesn't exist for arrays, right?

spir denis.spir at gmail.com
Tue Mar 8 11:31:12 PST 2011


On 03/08/2011 06:56 PM, Andrej Mitrovic wrote:
> 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.

Agreed. But do you understand why dmd throws that error, anyway? I'm not sure, 
the following may be plain shit. My guess is, since "UFCS" (universal function 
call syntax) exists for arrays, when dmd decodes "data.empty" and does not find 
any "empty" slot on 'data' or on its type, it tries rewriting it into 
"empty(data)". Right? then, to execute that, it looks for an "empty" func in 
the module, which it does not find... thus the message.
Note that if one of your imports happened to hold an "empty" func, either it 
would execute by plain chance, or you would get a type error!
HTH

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list