Bug 15926 – Peculiar behavior of 'inout'

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-04-15T06:47:42Z
Last change time
2022-11-07T10:18:29Z
Assigned to
No Owner
Creator
Walter Bright

Comments

Comment #0 by bugzilla — 2016-04-15T06:47:42Z
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?
Comment #1 by k.hara.pg — 2016-04-15T10:52:29Z
Comment #2 by schveiguy — 2016-04-15T15:28:52Z
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.
Comment #3 by dfj1esp02 — 2016-04-18T14:26:12Z
https://github.com/dlang/phobos/pull/3520 - somewhat related pull to improve concept checks.
Comment #4 by razvan.nitu1305 — 2022-11-07T10:18:29Z
This report is not an actual bug report. `inout` is working as expected in this case.