How useful should inout be?

Infiltrator Lt.Infiltrator at gmail.com
Sun Mar 23 03:28:52 PDT 2014


So, following on from monarchdodra's comment [0] in the bug 
tracker, how exactly should inout work?  For example, should the 
following work?

----------------------------------------------------
import std.algorithm : map;

class L {
    auto fun(const S s) inout nothrow pure @safe {
       if(point[0] is s)
          return point[1];
       else
          return point[0];
    }
    this(S a, S b) {
       point = [a, b];
    }
    S[2] point;
}

class S {
    @property auto foo() inout nothrow pure @safe {
       return arr.map!(e => e.fun(this));
    }
    L[] arr;
}

void main() { }
----------------------------------------------------


Writing foo imperatively causes no problems with inout:
----------------------------------------------------
    @property auto foo() inout nothrow pure @safe {
       inout(S)[] tmp;
       foreach(e; arr)
          tmp ~= e.fun(this);
       return tmp;
    }
----------------------------------------------------


Of course, the functional style looks cleaner, neater, and more 
immediately obvious what is being done.

So, is this a limitation with inout, part of its design, or am I 
misunderstaning something more fundamental?


[0] https://d.puremagic.com/issues/show_bug.cgi?id=12408#c4


More information about the Digitalmars-d-learn mailing list