www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias this + std.array.popFront

reply "Tobias Pankrath" <tobias pankrath.net> writes:
Hello,

the following struct is not an input range because you can't call 
popFront on it, however I'd say you should be able to.

struct Wrapper {
     int[] _data;
     alias _data this;
}

Bug or intended behaviour?

Thank you!
Mar 29 2013
parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
On Friday, 29 March 2013 at 22:13:31 UTC, Tobias Pankrath wrote:
 Hello,

 the following struct is not an input range because you can't 
 call popFront on it, however I'd say you should be able to.

 struct Wrapper {
     int[] _data;
     alias _data this;
 }

 Bug or intended behaviour?

 Thank you!
The reason seems to be that, while is(DynamicArrayTypeOf!Wrapper == int[]) holds, isDynamicArray!Wrapper equals false, because isDynamicArray checks for !isAggregateType!Wrapper. Looks like a bug isDynamicArray to me.
Mar 29 2013
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, March 29, 2013 23:22:36 Tobias Pankrath wrote:
 On Friday, 29 March 2013 at 22:13:31 UTC, Tobias Pankrath wrote:
 Hello,
 
 the following struct is not an input range because you can't
 call popFront on it, however I'd say you should be able to.
 
 struct Wrapper {
 
 int[] _data;
 alias _data this;
 
 }
 
 Bug or intended behaviour?
 
 Thank you!
The reason seems to be that, while is(DynamicArrayTypeOf!Wrapper == int[]) holds, isDynamicArray!Wrapper equals false, because isDynamicArray checks for !isAggregateType!Wrapper. Looks like a bug isDynamicArray to me.
No, it is most definitely _not_ a bug in isDynamicArray. The isX traits in std.traits specifically check for exact tyes and _not_ implicit conversions. If it's a bug, it's in the fact that isDynamicArray is used in popFront's template constraint. Whether or not Wrapper should be treated as a range is up for some debate (implicit conversion tends to cause big problems with template constraints, as it becomes far too easy to have constraints which pass and yet the function doesn't actually work with the type), but the main problem is that front and popFront for arrays don't agree on what they should work with - front takes T[] and popFront checks isDynamicArray. So, front works with implicit conversion, and popFront doesn't, and that needs to be fixed. But the bug is in front/popFront, not isDynamicArray. I believe that there was a thread on this recently (started by monarch_dodra IIRC), so you can look for that discussion for more information if you'd like. - Jonathan M Davis
Mar 29 2013
next sibling parent "Tobias Pankrath" <tobias pankrath.net> writes:
On Saturday, 30 March 2013 at 00:23:17 UTC, Jonathan M Davis 
wrote:
 On Friday, March 29, 2013 23:22:36 Tobias Pankrath wrote:
 On Friday, 29 March 2013 at 22:13:31 UTC, Tobias Pankrath 
 wrote:
 Hello,
 
 the following struct is not an input range because you can't
 call popFront on it, however I'd say you should be able to.
 
 struct Wrapper {
 
 int[] _data;
 alias _data this;
 
 }
 
 Bug or intended behaviour?
 
 Thank you!
The reason seems to be that, while is(DynamicArrayTypeOf!Wrapper == int[]) holds, isDynamicArray!Wrapper equals false, because isDynamicArray checks for !isAggregateType!Wrapper. Looks like a bug isDynamicArray to me.
No, it is most definitely _not_ a bug in isDynamicArray. The isX traits in std.traits specifically check for exact tyes and _not_ implicit conversions. If it's a bug, it's in the fact that isDynamicArray is used in popFront's template constraint. Whether or not Wrapper should be treated as a range is up for some debate (implicit conversion tends to cause big problems with template constraints, as it becomes far too easy to have constraints which pass and yet the function doesn't actually work with the type), but the main problem is that front and popFront for arrays don't agree on what they should work with - front takes T[] and popFront checks isDynamicArray. So, front works with implicit conversion, and popFront doesn't, and that needs to be fixed. But the bug is in front/popFront, not isDynamicArray. I believe that there was a thread on this recently (started by monarch_dodra IIRC), so you can look for that discussion for more information if you'd like. - Jonathan M Davis
Yes, you are most probably right that the bug is not in isDynamicArray. However DynamicArrayTypeOf and isDynamicArray are rather inconsistent since DynamicArrayTypeOf does not check exact types.
Mar 30 2013
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 30 March 2013 at 00:23:17 UTC, Jonathan M Davis 
wrote:
 I believe that there was a thread on this recently (started by 
 monarch_dodra
 IIRC), so you can look for that discussion for more information 
 if you'd like.
http://forum.dlang.org/thread/xhzojyrrcidusvreupsc forum.dlang.org "alias this and array popFront: is this a bug?" The similarity in code snippet is uncanny. Kenji seems to have jumped on the issue, and fixed it exactly according to suggestion.
Mar 30 2013