January 10, 2014
Hi,

I read about inout functions in the language documentation which allows to use inout in order to set the constness of the return type based on the constness of some argument. What is not mentioned is that this also works for the this-reference by marking the function itself inout:

So instead of

class C
{
  OtherClass foo;

  const(OtherClass) getOther() const { return foo; }
  OtherClass getOther() { return foo; }
}

we can write

class C
{
  OtherClass foo;

  inout(OtherClass) getOther() inout { return foo; }
}

Since the documentation only talks about marking arguments inout, can I nevertheless rely on its behavior? Anyways, I think it is worth to be included in the docs.

Best regards,

Matthias
January 11, 2014
On Friday, 10 January 2014 at 11:03:58 UTC, Matthias Walter wrote:
> Since the documentation only talks about marking arguments inout, can I
> nevertheless rely on its behavior? Anyways, I think it is worth to be
> included in the docs.

The implicit this-reference parameter is just that - another parameter. Any type constructor/qualifier can be applied to it, as with other parameters. Yes, you can rely on the behaviour. Use of `inout` on the this-reference is one of the most common uses of `inout`, as warranted by the popularity of member functions.

It is technically part of the documentation[1], but I agree that this use case should probably be included in the description of `inout`.

[1] http://dlang.org/declaration#MemberFunctionAttributes