www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Empty field doesn't exist for arrays, right?

reply Andrej Mitrovic <none none.none> writes:
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. 
Mar 08 2011
next sibling parent Andrej Mitrovic <none none.none> writes:
Nevermind, I'm dumb. It's in std.array, I just need to import it. This needs to
be said in TDPL however.
Mar 08 2011
prev sibling next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Mar 08 2011
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/8/11, bearophile <bearophileHUGS lycos.com> wrote:
 empty is not an array method, it's a free function that is used with a
 funny syntax.
Yes but the compiler doesn't know that until std.array is imported. A better error message is that empty isn't a property of that array. It's much easier to reason about this way.
Mar 08 2011
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Andrej Mitrovic Wrote:

 On 3/8/11, bearophile <bearophileHUGS lycos.com> wrote:
 empty is not an array method, it's a free function that is used with a
 funny syntax.
Yes but the compiler doesn't know that until std.array is imported. A better error message is that empty isn't a property of that array. It's much easier to reason about this way.
What if you are trying to create a method which will act as a property for the array? If you get it wrong you would get the error that an array doesn't have the property and scream, "I know that is why I'm building a function for it. Why won't it find my function stead of looking at what array provides!" Also doesn't TDPL introduce Ranges by implementing them for arrays and then tell you that you don't have to do this every time because it is an the standard labrary std.array?
Mar 08 2011
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/8/11, Jesse Phillips <jessekphillips+D gmail.com> wrote:
 What if you are trying to create a method which will act as a property for
 the array? If you get it wrong you would get the error that an array doesn't
 have the property and scream, "I know that is why I'm building a function
 for it. Why won't it find my function stead of looking at what array
 provides!"
But wouldn't this case be much more obvious with the property error message? Or maybe it wouldn't.. all I know is I got bitten by this error message a couple of times and it always got me that WTF look on my face. How common are typos in invoking methods versus typos in implementing methods like a UFCS function?
Mar 08 2011
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Andrej Mitrovic Wrote:

 On 3/8/11, Jesse Phillips <jessekphillips+D gmail.com> wrote:
 What if you are trying to create a method which will act as a property for
 the array? If you get it wrong you would get the error that an array doesn't
 have the property and scream, "I know that is why I'm building a function
 for it. Why won't it find my function stead of looking at what array
 provides!"
But wouldn't this case be much more obvious with the property error message? Or maybe it wouldn't.. all I know is I got bitten by this error message a couple of times and it always got me that WTF look on my face.
I don't think so. If you didn't know empty was a library feature then you'd get the message, "empty is not a property of array." and be confused thinking that other code uses it. It would probably result in looking up other code which uses it then the properties available to arrays in the documentation and then a post to the NG confused on how other code gets away with it. I don't really know, it just seems like someone is going to be lost no matter what it is.
 How common are typos in invoking methods versus typos in implementing
 methods like a UFCS function?
But if you know that empty is a function not a property, then the error makes much more sense. I think the confusion comes from not knowing it is a property or a function you are trying to call.
Mar 08 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Well, maybe one day we'll have a compiler with lots of front-end
customization options (or some nice analysis tools). In any case this

Mar 08 2011
prev sibling parent spir <denis.spir gmail.com> writes:
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
Mar 08 2011