View mode: basic / threaded / horizontal-split · Log in · Help
October 05, 2012
is array an InputRange?
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?
October 05, 2012
Re: is array an InputRange?
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?

Because you specified int[] element type and cast operator returns null 
when objects don't implement requested interface. You need to specify 
int as element type:

auto inputRange = cast(InputRange!int)rangeObject;

Anyway you don't need to cast to InputRange(E) interface. You can use 
rangeObject directly.
October 05, 2012
Re: is array an InputRange?
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
October 05, 2012
Re: is array an InputRange?
On Friday, 5 October 2012 at 13:39:56 UTC, 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?

On another note to what others said. An array is only a range 
when std.array has been imported (or passed to a module expecting 
a range which imports std.array).

A range is only a concept, it can not be be stored in an input 
range variable. inputRangeObject is rarely needed and I can't 
give an example of when it would be.
October 06, 2012
Re: is array an InputRange?
On Friday, 5 October 2012 at 13:39:56 UTC, 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?

Suggested reading:

http://ddili.org/ders/d.en/ranges.html

Forget about the std.range objects.

NMS
Top | Discussion index | About this forum | D home