Array types not treated uniformly when passed as ranges

jam gr0v3er at gmail.com
Mon Feb 14 18:18:39 PST 2011


Hi all,

Just curious as to the difference in the built-in variable length
array vs. the std.container.Array and fixed length arrays when it
comes to using them in functions that take Ranges.

For instance the following does not compile:

import std.algorithm;
import std.stdio;
import std.range;
import std.conv;
import std.container;
import std.array;

void main() {

    int[5] builtin_fixed;
    int[] builtin_variable;
    Array!(int) con_array;

    con_array.length(5);
    builtin_variable.length = 5;

    fill(builtin_variable, 9); //ok, no error
    isSorted(builtin_variable); //ditto

    //The following 4 statements produce errors
    fill(builtin_fixed, 9);
    fill(con_array, 9);

    isSorted(con_array);
    isSorted(builtin_fixed);

}

The errors are variations on:

Error: template std.algorithm.fill(Range,Value) if
(isForwardRange!(Range) && is(typeof(range.front = filler))) does not
match any function template declaration
Error: template std.algorithm.fill(Range,Value) if
(isForwardRange!(Range) && is(typeof(range.front = filler))) cannot
deduce template function from argument types !()(int[5LU],int)

If I change those 4 statements to:

    fill(builtin_fixed[], 9);
    fill(con_array[], 9);

    isSorted(con_array[]);
    isSorted(builtin_fixed[]);

effectively passing ranges (std.container.Array!(int).Array.Range in
the case of con_array, and int[] for builtin_fixed) which then works
as expected.  This all makes sense, and it's easy enough to write
wrappers,   but I would (well and I did) expect the first way to just
work.   This may just be a nitpick I guess, but being new to the
language this little detour involved quite a bit of time research (not
a bad thing, I did learn quite a bit in the process), but makes me
wonder if I am missing something fundamental regarding when I should
be using these different array types.

Cheers


More information about the Digitalmars-d-learn mailing list