making a struct an inputRange with free functions

jmh530 john.michael.hall at gmail.com
Wed Apr 18 20:39:46 UTC 2018


On Monday, 16 April 2018 at 19:27:28 UTC, Jonathan M Davis wrote:
> [snip]

It really would be nice if it worked with free functions...

I was trying to get the example working with Atila's concepts 
library [1]. I tried re-writing the checkInputRange function so 
that UFCS is only used when hasMember passes, but that didn't 
seem to help things. The big issue is when the free functions are 
in another module that checkInputRange does not know about. The 
only solution I can think of is a template mixin (below). Then to 
use, you import InputRange; mixin InputRange; . However, I have 
no idea how well this will work more generally.



mixin template InputRange()
{
     void checkInputRange(R)(inout int = 0) {
         R r = R.init;     // can define a range object
         if (r.empty) {}   // can test for empty
         r.popFront;       // can invoke popFront()
         auto h = r.front; // can get the front of the range
     }
     enum isInputRange(R) = is(typeof(checkInputRange!R));
}

[1] https://github.com/atilaneves/concepts


More information about the Digitalmars-d-learn mailing list