inout after function

Ali Çehreli acehreli at yahoo.com
Sat Nov 25 21:59:54 UTC 2017


On 11/25/2017 01:51 PM, Dave Jones wrote:
 > What does the "inout" after front() do here...
 >
 >
 > @property ref inout(T) front() inout
 > {
 >      assert(_data.refCountedStore.isInitialized);
 >      return _data._payload[0];
 > }
 >
 > Cant seem to find an explanation in the docs or forums :(

It's for member functions. Without it, and if you needed, you would have 
to write separate functions for mutable, const, and immutable objects of 
that type.

For example, the following function works for all three qualifications. 
It won't compile if you remove that inout:

struct S {
     int i;

     @property ref inout(int) front() inout {
         return i;
     }
}

void main() {
     auto m = S(1);
     auto c = const(S)(2);
     static assert(is(typeof(m.front) == int));
     static assert(is(typeof(c.front) == const(int)));
}

Ali


More information about the Digitalmars-d-learn mailing list