www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - inout after function

reply Dave Jones <dave jones.com> writes:
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 :(
Nov 25 2017
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Nov 25 2017
parent reply Dave Jones <dave jones.com> writes:
On Saturday, 25 November 2017 at 21:59:54 UTC, Ali Çehreli wrote:
 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
So it makes it a const/immutable/mutable method depending on whether the instance it is called on is const/immutable/mutable? So
      property ref inout(int) front() inout {
         return i++;
     }
Would fail if you called it on an immutable instance of S.
Nov 25 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 26 November 2017 at 01:35:01 UTC, Dave Jones wrote:
 So it makes it a const/immutable/mutable method depending on 
 whether the instance it is called on is const/immutable/mutable?
On the outside, yes.
 So

      property ref inout(int) front() inout {
         return i++;
     }
Would fail if you called it on an immutable instance of S.
That wouldn't compile in any case: on the inside of the function, inout == const (this is the only way the one function can be used for all three). The inout propagation is just seen at the call site.
Nov 25 2017
parent Dave Jones <dave jones.com> writes:
On Sunday, 26 November 2017 at 04:51:08 UTC, Adam D. Ruppe wrote:
 On Sunday, 26 November 2017 at 01:35:01 UTC, Dave Jones wrote:
 So it makes it a const/immutable/mutable method depending on 
 whether the instance it is called on is 
 const/immutable/mutable?
On the outside, yes.
 So

      property ref inout(int) front() inout {
         return i++;
     }
Would fail if you called it on an immutable instance of S.
That wouldn't compile in any case: on the inside of the function, inout == const (this is the only way the one function can be used for all three). The inout propagation is just seen at the call site.
Ahh ok, makes sense now.
Nov 26 2017
prev sibling next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 25 November 2017 at 21:51:41 UTC, Dave Jones wrote:
 What does the "inout" after front() do here...
Applies the `inout` modifier to the hidden `this` variable inside the function. https://dlang.org/spec/function.html#inout-functions It basically makes it const inside the function, but on the outside, it matches whatever the constness was of the object it is called on.
Nov 25 2017
prev sibling parent Guillaume Piolat <first.last gmail.com> writes:
On Saturday, 25 November 2017 at 21:51:41 UTC, 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 :(
https://p0nce.github.io/d-idioms/#Knowing-inout-inside-out
Nov 26 2017