Thread overview
inout functions
Aug 23, 2012
Piotr Szturmaj
Aug 23, 2012
Timon Gehr
Aug 23, 2012
Piotr Szturmaj
Aug 24, 2012
Kagamin
August 23, 2012
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?
August 23, 2012
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).

August 23, 2012
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

August 24, 2012
Well, it may be also a way to ensure the function doesn't modify the struct.