Empty field doesn't exist for arrays, right?
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.
Re: Empty field doesn't exist for arrays, right?
Nevermind, I'm dumb. It's in std.array, I just need to import it. This needs to be said in TDPL however.
Re: Empty field doesn't exist for arrays, right?
On 3/8/11, bearophile bearophileh...@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.
Re: Empty field doesn't exist for arrays, right?
Andrej Mitrovic Wrote: On 3/8/11, bearophile bearophileh...@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?
Re: Empty field doesn't exist for arrays, right?
On 3/8/11, Jesse Phillips jessekphillip...@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?
Re: Empty field doesn't exist for arrays, right?
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
Re: Empty field doesn't exist for arrays, right?
Andrej Mitrovic Wrote: On 3/8/11, Jesse Phillips jessekphillip...@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.
Re: Empty field doesn't exist for arrays, right?
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 topic is done here since post #2. :)