making a struct an inputRange with free functions

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 16 19:27:46 UTC 2018


On 4/16/18 3:10 PM, Johannes Loher wrote:
> Is there a way to do this? Here is a naive implementation:
> https://run.dlang.io/is/JKvL80 .
> 
> It does not pass `isInputRange` (I think, because the free functions are
> not visible in the scope of `isInputRange`).

You are correct, it's not possible.

> Trying to iterate over it with a foreach loop results in a compile error:
> Error: invalid foreach aggregate NoRange(0, 0).this(5), define
> opApply(), range primitives, or use .tupleof

The compiler actually looks to see if it has front as a member.

If it has that one function, the foreach is attempted.

https://run.dlang.io/is/Yzn3ra

But this is an implementation detail. It may not be valid in future 
versions of the compiler. Note, I had to add a ref to your popFront, or 
it's an infinite loop :)

-Steve


More information about the Digitalmars-d-learn mailing list