www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - inout functions

reply Piotr Szturmaj <bncrbme jadamspam.pl> writes:
Hi,

I found this code of std.range.iota's Result struct:

          property inout(Value) front() inout { assert(!empty); return 
current; }

What's the purpose of inout on parameterless functions?
Aug 23 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 08/24/2012 12:14 AM, Piotr Szturmaj wrote:
 Hi,

 I found this code of std.range.iota's Result struct:

           property inout(Value) front() inout { assert(!empty); return
 current; }

 What's the purpose of inout on parameterless functions?
It is a method of a struct, therefore it is not parameterless, but has a hidden parameter. 'inout' qualifies the implicit 'this' reference. Inside the method body, typeof(this) is inout(Result).
Aug 23 2012
parent reply Piotr Szturmaj <bncrbme jadamspam.pl> writes:
Timon Gehr wrote:
 On 08/24/2012 12:14 AM, Piotr Szturmaj wrote:
 Hi,

 I found this code of std.range.iota's Result struct:

           property inout(Value) front() inout { assert(!empty); return
 current; }

 What's the purpose of inout on parameterless functions?
It is a method of a struct, therefore it is not parameterless, but has a hidden parameter. 'inout' qualifies the implicit 'this' reference. Inside the method body, typeof(this) is inout(Result).
Thank you. So, it's helpful because struct might be qualified somewhere as const or as immutable. Anyway in that particular case it's unnecessary because iota's Result must be mutable to call popFront(). immutable iotaRange = iota(0, 5); pragma(msg, typeof(&iotaRange.front)); pragma(msg, typeof(iotaRange.front)); io.popFront(); yields: inout(int) delegate() inout property immutable(int) main.d(61): Error: function std.range.iota!(int,int).iota.Result.popFront () is not callable using argument types () immutable
Aug 23 2012
parent "Kagamin" <spam here.lot> writes:
Well, it may be also a way to ensure the function doesn't modify 
the struct.
Aug 24 2012