is array an InputRange?
Ali Çehreli
acehreli at yahoo.com
Fri Oct 5 06:54:45 PDT 2012
The short answer is yes, slices satisfy the requirements of InputRange.
(And all the other ranges as well.)
There is a distinction between an array and a slice and what you hvae in
your code are slices anyway. Dynamic arrays are owned by the runtime.
What we get is a slice to the elements of arrays.
In the case of static arrays, no, they are not ranges. Bug again, a
slice to the elements of it is a range:
int[3] sArray; // static array is not a range
int[] slice = sArray; // slice is a range
On 10/05/2012 06:28 AM, ref2401 wrote:
> import std.range;
>
> int[] numbers = [1, 3, 5, 7, 9, 11];
>
> auto rangeObject = inputRangeObject(numbers);
> auto inputRange = cast(InputRange!(int[]))rangeObject;
>
> why does 'inputRange' equal null?
>
They don't have compatible types:
pragma(msg, typeof(rangeObject));
pragma(msg, typeof(inputRange));
The output is
InputRangeObject
InputRange
Those two are not in the same class hierarchy and they should not be
expected to be so either. InputRange is a concept. We can say
isInputRange!Foo but there is no useful type InputRange!Foo.
inputRangeObject on the other hand is a helper function that allows
normally incompatible ranges to be use in a compatible way as "a range
of a certain element type" as in "a range of ints". For example, two
incompatible ranges can both be seen as "a range of ints".
Having said all of that, what are you trying to do? :) You can simply do
this:
auto inputRange = rangeObject;
If you want to ensure that the inputRange variable above satisfy the
InputRange concept, do this:
static assert (isInputRange!(typeof(inputRange)));
Ali
More information about the Digitalmars-d-learn
mailing list