isInputRange copied verbatim produces a different result than isInputRange from std.range

arturg var.spool.mail700 at gmail.com
Sun Mar 4 21:03:23 UTC 2018


On Sunday, 4 March 2018 at 19:58:14 UTC, ag0aep6g wrote:
> On 03/04/2018 08:54 PM, aliak wrote:
>> wait a minute... so I can't use any std.range functions on a 
>> type if I add the range primitives as free functions? O.o
>
> Yes. In other words: You can't implement range primitives as 
> free functions. Because std.range (and std.algorithm, etc.) 
> doesn't know about them.

isn't this what DIP 1005 tried to solve?

as long as you dont want to role your own test its not possible.

module moda;

struct Imports
{
     string importString;
}

template isInputRange(R)
{
     import std.traits: hasUDA, getUDAs, ReturnType;

     static if(hasUDA!(R, Imports))
     static foreach(u; getUDAs!(R, Imports))
     mixin(u.importString);

     enum bool isInputRange =
         is(typeof(R.init) == R)
         && is(ReturnType!((R r) => r.empty) == bool)
         && is(typeof((return ref R r) => r.front))
         && !is(ReturnType!((R r) => r.front) == void)
         && is(typeof((R r) => r.popFront));
}

-----

module test;

import std.stdio;
import moda;

void main(string[] args)
{
     isInputRange!Type.writeln;
}

bool empty(Type t) { return true; }
int front(Type t) { return 42; }
void popFront(Type t) {}

@Imports("import test: empty, front, popFront;")
struct Type
{
}




More information about the Digitalmars-d-learn mailing list