Discussion:
http://www.digitalmars.com/d/archives/digitalmars/D/So_what_does_inout_int_0_do_283407.html
Use case:
https://github.com/D-Programming-Language/phobos/blob/master/std/range/primitives.d#L152
Given the code:
template isInputRange(R)
{
enum bool isInputRange = is(typeof(
(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
}));
}
///
@safe unittest
{
struct A {}
struct B
{
void popFront();
@property bool empty();
@property int front();
}
static assert(!isInputRange!A);
static assert( isInputRange!B);
static assert( isInputRange!(int[]));
static assert( isInputRange!(char[]));
static assert(!isInputRange!(char[4]));
static assert( isInputRange!(inout(int)[]));
}
What is the 'inout int = 0' doing there and why does 'static assert( isInputRange!(inout(int)[]));' fail when it is removed? There seems to be some sort of undocumented behavior going on?
The issue is that inout local variables are only allowed to be declared inside functions that accept inout parameters.
For example:
void foo()
{
inout int x; // error
}
Note, this is not a bug, but expected behavior, just not well-explained. However, I think we should remove the limitation, as it causes more problems than it solves.