Why are compile-time constraints checked with an (inout int = 0) lambda in Phobos?

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Mar 28 04:43:25 PDT 2014


On 3/27/14, Atila Neves <atila.neves at gmail.com> wrote:
> Why the (inout int = 0) instead of an empty parameter list?

Try removing it and compile std.range with -unittest. Here's what happens:

std\range.d(546): Error: static assert  (isInputRange!(inout(int)[])) is false

The reason it's false is because the code wouldn't compile. Here's a test-case:

-----
import std.array;

void test()
{
    inout(int)[] r = (inout(int)[]).init;
    if (r.empty) {}
    r.popFront();
    auto h = r.front;
}

void main()
{
    test();
}
-----

test.d(7): Error: variable test.test.r inout variables can only be
declared inside inout functions

So you need to add inout in the parameter list to avoid this compiler
error and to make your function work with any qualified range type.


More information about the Digitalmars-d-learn mailing list