inout after function
Dave Jones
dave at jones.com
Sun Nov 26 01:35:01 UTC 2017
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.
More information about the Digitalmars-d-learn
mailing list