Interfaces, traits, concepts, and my idea for a DIP
Piotrek via Digitalmars-d
digitalmars-d at puremagic.com
Sat Aug 1 05:10:47 PDT 2015
On Friday, 31 July 2015 at 16:28:30 UTC, jmh530 wrote:
> Looking at the PR also resolved my earlier question. Running
> the code as below (do not import std.range) will tell you
> exactly what isn't implemented from isInputRange (in this case,
> I commented out popFront). Very cool.
>
> template satisfies(alias Constraint, R) {
> enum check = "check" ~ Constraint.stringof[2..$-3] ~ "!(R)";
> enum assert_ = "static assert("~ check ~ ");";
> mixin(assert_); //mixes in "static
> assert(checkInputRange!R)"
> }
>
> template isInputRange(R)
> {
> enum bool isInputRange = is(typeof(checkInputRange!R));
> }
>
> bool checkInputRange(R)(inout int = 0)
> {
> if (__ctfe)
> {
> 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
> }
> return true;
> }
>
> @satisfies!(isInputRange, Zeroes)
> struct Zeroes {
> enum empty = false;
> //void popFront() {}
> @property int front() { return 0; }
> }
>
> void main()
> {
> Zeroes Z;
> }
Nice. Here are the actual error messages for the record:
/d125/f236.d(18): Error: no property 'popFront' for type 'Zeroes'
/d125/f236.d(24): Error: template instance
f236.satisfies!(isInputRange, Zeroes) error instantiating
Piotrek
More information about the Digitalmars-d
mailing list