www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - isRandomAccessRange on static array

reply "Brad Anderson" <eco gnuk.net> writes:
A user in IRC was having difficulty using std.algorithm.sort on a 
static array.  It appears to be due to static arrays don't pass 
the isRandomAccessRange constraint.

     static assert(isRandomAccessRange!(uint[5]));

Results in:

     Error: static assert  (isRandomAccessRange!(uint[5u])) is 
false

Is this a bug or am I missing something?

Regards,
Brad Anderson
Apr 09 2012
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/09/2012 03:20 PM, Brad Anderson wrote:
 A user in IRC was having difficulty using std.algorithm.sort on a static
 array. It appears to be due to static arrays don't pass the
 isRandomAccessRange constraint.

 static assert(isRandomAccessRange!(uint[5]));

 Results in:

 Error: static assert (isRandomAccessRange!(uint[5u])) is false

 Is this a bug or am I missing something?

 Regards,
 Brad Anderson
Static arrays cannot be even InputRanges because they can't provide popFront(). But a whole slice of a static array is even a RandomAccessRange: int[3] s; s[]; // whole slice Ali
Apr 09 2012
prev sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Tuesday, April 10, 2012 00:20:20 Brad Anderson wrote:
 A user in IRC was having difficulty using std.algorithm.sort on a
 static array. It appears to be due to static arrays don't pass
 the isRandomAccessRange constraint.
 
 static assert(isRandomAccessRange!(uint[5]));
 
 Results in:
 
 Error: static assert (isRandomAccessRange!(uint[5u])) is
 false
 
 Is this a bug or am I missing something?
Static arrays are not ranges: http://stackoverflow.com/questions/8873265/is-a-static-array-a-forward-range If you want to pass a static array to a range-based function, you must slice it first. - Jonathan M Davis
Apr 09 2012
parent "Brad Anderson" <eco gnuk.net> writes:
On Monday, 9 April 2012 at 23:22:57 UTC, Jonathan M Davis wrote:
 On Tuesday, April 10, 2012 00:20:20 Brad Anderson wrote:
 A user in IRC was having difficulty using std.algorithm.sort 
 on a
 static array. It appears to be due to static arrays don't pass
 the isRandomAccessRange constraint.
 
 static assert(isRandomAccessRange!(uint[5]));
 
 Results in:
 
 Error: static assert (isRandomAccessRange!(uint[5u])) is
 false
 
 Is this a bug or am I missing something?
Static arrays are not ranges: http://stackoverflow.com/questions/8873265/is-a-static-array-a-forward-range If you want to pass a static array to a range-based function, you must slice it first. - Jonathan M Davis
Ah, ok. That makes sense. Thanks. Regards, Brad Anderson
Apr 09 2012