Protocols vs Concepts?
Jesse Phillips
Jesse.K.Phillips+D at gmail.com
Wed Nov 20 22:49:53 PST 2013
On Wednesday, 20 November 2013 at 23:44:53 UTC, Shammah
Chancellor wrote:
> Wrap does something similar, but more (since I don't want a
> wrapper). I just want to check it at compile time. Something
> like this should be standard instead of isInputRange!() and the
> plethora of "concept checkers" in various libraries. It seems
> like we're developing a web of these concept-checkers and it's
> hard to see what I should implement to be an "InputRange" from
> the documentation. If there was a standard interface for it,
> and a library-defined checker, it would be more consistent
> across the codebase and easier to see what specifically needs
> to be implemented.
Well, if wrap functioned with structs then templates could use
(probably with helper template)
void foo(R)() if(is(typeof(wrap!Interface(R))) {
}
> it's hard to see what I should implement to be an "InputRange"
> from the documentation.
I disagree http://dlang.org/phobos/std_range.html#.isInputRange
r.empty returns false iff there is more data available in the
range.
r.front returns the current element in the range. It may
return by value or by reference. Calling r.front is allowed only
if calling r.empty has, or would have, returned false.
r.popFront advances to the next element in the range. Calling
r.popFront is allowed only if calling r.empty has, or would have,
returned false.
Seems really specific about what is needed and a quick:
unittest {
static assert(isInputRange!MyType);
}
Gives a validation check. There are range interfaces in std.range
already, they just don't include the bear minimum.
Using traits provides more power than a simple interface, but
sometimes an interface is all you need and should exist anyway.
In those cases an easy trait that checks for the type matches the
interface would be best.
More information about the Digitalmars-d
mailing list